Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creation of data.table using a list #968

Closed
asantucci opened this issue Nov 24, 2014 · 6 comments
Closed

Creation of data.table using a list #968

asantucci opened this issue Nov 24, 2014 · 6 comments

Comments

@asantucci
Copy link

This is a very similar issue as what is mentioned here: http://stackoverflow.com/questions/18390947/data-table-of-table-is-very-different-from-data-frame-of-table

yet this pertains to creating data.tables using a list input. For example:

### Data.frame takes in a list of 3 3x3 matrices, and creates a 3x9 data.frame.
data.frame(replicate(3, matrix(rnorm(9), nrow = 3)))

### Data.table takes in the same list, and creates a 27x1 data.table.
data.table(replicate(3, matrix(rnorm(9), nrow = 3)))

I discovered this behavior at least a year ago when I was trying to create a data.table object using mget

a <- letters
b <- LETTERS
vars <- c("a", "b")

### Works
data.frame(mget(vars))

### Doesn't work
data.table(mget(vars))

There are probably really good reasons for having this behavior that I am not aware of as a user, but at the same time it would be great to have data.table behaving as similarly as possible to data.frame!

Thanks for such a great package! I haven't used data.frame in years, and couldn't be happier.

@arunsrinivasan
Copy link
Member

Thanks. Could you please read completely and follow the instructions here?

Add the output you get wherever possible, comment the output, tell us which version you're using etc..

@richierocks
Copy link

It's worth noting that as.data.table behaves as expected; it is only data.table that behaves differently.

l <- list(x = 1:5, y = rnorm(5), z = letters[1:5])
as.data.frame(l)
##   x           y z
## 1 1 -0.09738263 a
## 2 2  1.47511422 b
## 3 3  0.22953616 c
## 4 4  0.08974962 d
## 5 5  1.21911408 e
data.frame(l)
##   x           y z
## 1 1 -0.09738263 a
## 2 2  1.47511422 b
## 3 3  0.22953616 c
## 4 4  0.08974962 d
## 5 5  1.21911408 e
as.data.table(l)
##    x           y z
## 1: 1 -0.09738263 a
## 2: 2  1.47511422 b
## 3: 3  0.22953616 c
## 4: 4  0.08974962 d
## 5: 5  1.21911408 e
data.table(l)
##                                                              l
## 1:                                                   1,2,3,4,5
## 2: -0.09738263, 1.47511422, 0.22953616, 0.08974962, 1.21911408
## 3:                                                   a,b,c,d,e

Tested with today's devel version of data.table.

@jangorecki
Copy link
Member

data.table behave differently than data.frame but still behave as expected.
What you do by data.table(l) is creating 1 column data.table which contains a list type column.
How would you create below data.table if it wouldn't behave as it is now?

structure(list(l = list(1:5, c(0.182844649428989, 0.858360447353789, 
-0.0532774982161298, 0.459157422043729, 0.822852400527859), c("a", 
"b", "c", "d", "e"))), .Names = "l", row.names = c(NA, -3L), class = c("data.table", 
"data.frame"))

@asantucci
Copy link
Author

as.data.table is what I was looking for.

Thank you so much for all the follow up. I agree that the behavior is as
expected for both data.table and as.data.table.

Thank you for maintaining such an excellent package! It's really my
favorite part of R.

On Wed, Dec 10, 2014 at 1:46 PM, Jan Gorecki [email protected]
wrote:

data.table behave differently than data.frame but still behave as
expected.
What you do by datat.table(l) is creating 1 column data.table which
contains a list column.
How would would create below data.table if it wouldn't behave as it is now?

structure(list(l = list(1:5, c(0.182844649428989, 0.858360447353789, -0.0532774982161298, 0.459157422043729, 0.822852400527859), c("a", "b", "c", "d", "e"))), .Names = "l", row.names = c(NA, -3L), class = c("data.table", "data.frame"))


Reply to this email directly or view it on GitHub
#968 (comment)
.

@jangorecki
Copy link
Member

@asantucci just don't forget to close this issue (assuming it is resolved) when you will have access to github (I see you did reply via email).
PS. data.table is also my favorite part of R :)

@arunsrinivasan
Copy link
Member

I've noted this in #944 to expand on this point under FAQ. The underlying idea is to facilitate creation of list columns in a straightforward manner. If you've a list and would like to create a data.table then use as.data.table(); it's more appropriate.

Also closing #877.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants