Skip to content

Creation of data.table using a list #968

Description

@asantucci

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.

Activity

  1. arunsrinivasan commented on Dec 4, 2014

    @arunsrinivasan
    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..

  2. richierocks commented on Dec 10, 2014

    @richierocks

    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.

  3. jangorecki commented on Dec 10, 2014

    @jangorecki
    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"))
  4. asantucci commented on Dec 10, 2014

    @asantucci
    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)
    .

  5. jangorecki commented on Dec 10, 2014

    @jangorecki
    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 :)

  6. arunsrinivasan commented on Feb 8, 2015

    @arunsrinivasan
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions