Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdding list column to one row data table #3626
Comments
|
could you confirm you are on the most recent master (share sessionInfo())? a related bug was fixed recently |
|
if you are using devel version from our devel drat repos please provide data.table:::.git() |
|
Sorry. I always tend to forget. But yes. I just installed the latest version from github.
And the git version > utils::packageDescription("data.table")
Package: data.table
Version: 1.12.3
...
ByteCompile: TRUE
RemoteType: github
RemoteHost: api.github.com
RemoteRepo: data.table
RemoteUsername: Rdatatable
RemoteRef: master
RemoteSha: 421f672f72c8e8749a71cda121925e8f5be84242
GithubRepo: data.table
GithubUsername: Rdatatable
GithubRef: master
GithubSHA1: 421f672f72c8e8749a71cda121925e8f5be84242
NeedsCompilation: yes
Packaged: 2019-06-04 09:58:30 UTC; ...
...
Built: R 3.6.0; x86_64-pc-linux-gnu; 2019-06-04 09:58:33 UTC; unix |
|
adding extra library(data.table)
dt = data.table(a = 1)
list_column = list(list(a = 1, b = 2))
dt$b = list(list_column)
dt |
|
So would you say it's a bug or I always should add list columns with an extra |
|
Yes, it is a bug that it doesn't work for 1 row data.table while it does work for 2+ rows data.table. |
|
I am not sure if we can deal with it nicely. d = data.table
# ordinary syntax
d(a=1L)[, newcol := list(2L)][]
# because of helper we can drop `list` in RHS, and we still get the same outcome
d(a=1L)[, newcol := 2L][]
# same is true for 2+ rows DT, note that RHS is recycled
d(a=1:2)[, newcol := list(2L)][]
d(a=1:2)[, newcol := 2L][]
# problem starts where we want to have a list column to be used in RHS
# adding extra `list` to both calls will make outcome different
d(a=1L)[, newcol := list(list(2L))][] ## list column
d(a=1L)[, newcol := list(2L)][] ## int column
# it is the same for 1 row and 2+ rows DT
# when RHS list column has 1+ elements, it is interpreted as multiple columns to assign
# thus error reported by Jakob
d(a=1L)[, newcol := list(list(2L, 3L))][] ## error
d(a=1L)[, newcol := list(2L, 3L)][] ## error
d(a=1:2)[, newcol := list(list(2L, 3L))][] ## list column
d(a=1:2)[, newcol := list(2L, 3L)][] ## list columnI don't know if there is a way to reliably identify when user wants to use list column, or just use a syntax that doesn't require our internal helper list wrapper. |
I can not add a list column to a data.table with one row:
The following workaround works and prodcues the desired result.