-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Cannot add list column to empty data.table
#5738
Comments
I think the result makes sense; you just need to grad it into another list as you can see below.
|
@AngelFelizR I know how to work around the issue but library(data.table)
dt = data.table()
x = list(diag(2))
dt[, a := x][]
#> Warning in `[.data.table`(dt, , `:=`(a, x)): 2 column matrix RHS of := will be
#> treated as one vector
#> a
#> <num>
#> 1: 1
#> 2: 0
#> 3: 0
#> 4: 1
dt = data.table()
x = list(diag(2))
dt[, a := list(x)][]
#> a
#> <list>
#> 1: 1,0,0,1 |
You just need to be aware the error make sense in the default use of list that we use with lapply > dt = data.table()
> x = list("a", 1)
> dt[, c("a","b") := x]
> str(dt)
Classes ‘data.table’ and 'data.frame': 1 obs. of 2 variables:
$ a: chr "a"
$ b: num 1
- attr(*, ".internal.selfref")=<externalptr> |
I believe this issue may be caused by a automatic wrapping input on RHS (or What if we want to add list of lists column? or list of list of lists column? Atomic columns are automatically enlisted, that's why code below works x[ c("a","b") := list(1:2, 2:3)]
x[ c("a") := 1:2] to avoid this inherited ambiguity we would have to ask users to always write x[ c("a") := list(1:2)] I am afraid trying to fix this edge case we are just shifting problem to another edge case, but I am not against, as long as we have good unit tests included and revdeps passes. I may also be wrong, as the issue precisely mentions scalar columns, maybe this part we can fix without shifting problem elsewhere. |
That would be a breaking change and I am not sure if that would improve the general experience |
The current situation is the following: Case 1: Case 2: I want to extend Case 2a) to better distinguish whether a list column is added or multiple columns ( edit: What is still open are weird cases of trying to add columns to a 0-row |
Both inplace operations currently do not allow adding lists of lengths over 1. The problem arises on both
1.14.8
and current dev.Adding columns of other types works fine
The text was updated successfully, but these errors were encountered: