Skip to content

data.table(x) != as.data.table(x) #877

Description

@jamesonquinn

Here's the motivating example which had me mightily confused at first:

l = list(x=1,y=2)
data.frame(l)
#   x y
#1 1 2
as.data.table(l)
#    x y
#1: 1 2
data.table(l)
#    l
#1: 1
#2: 2

Now that I've explored this issue, I understand what's going on. But at the very least, there should be an entry in the faq about this.

The issue is that you might say data.table(listOne,listTwo) (or indeed, data.table(vectorOne,vectorTwo)) to create a two-column table, while as.data.table should only take one argument. Personally, I think the default here is wrong; it would make more sense, and be easier to troubleshoot bugs, if data.table(listOne) was the same as as.data.table(listOne), rather than being similar to data.table(listOne, listTwo).

This is touched on in the FAQ when it says "data.frame(list(1:2,"k",1:4)) creates 3 columns, data.table creates one list column." Still, I think that ideally the default should change: data.table with one argument should be the same as as.data.table. If not, this behavior should at least be better-documented.

Activity

  1. jangorecki commented on Jan 10, 2015

    @jangorecki
    Member

    @jamesonquinn please check related issue: #968 (comment)
    Do you have an idea how would be possible in such case to create a data.table with one list column? If you don't have an idea for this then I believe this issue can be closed as it has to be possible to create one list column data.table.

  2. added this to the v1.9.8 milestone on Feb 6, 2015
  3. modified the milestones: v1.9.6, v1.9.8 on Feb 7, 2015
  4. 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.

  5. arunsrinivasan commented on Feb 8, 2015

    @arunsrinivasan
    Member

    @jamesonquinn, Do you've any thoughts on how better to document it? Currently, the difference is stated clearly under "differences between data.frame and data.table. I can't think of a more appropriate section. I'm guessing you want to expand on the content?

  6. eantonya commented on Feb 8, 2015

    @eantonya
    Contributor

    Just for reference, list behaves similarly in base R. as.list(c(1:5)) and list(c(1:5)) yield very different results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions