Reputation: 5134
my question is an extension to the previously answered one here:
Add multiple columns to R data.table in one function call?
I'm having problems assigning multiple new columns if the data.table has a key according to which I subset. Example:
library(data.table)
example(data.table)
DT[J("a")]
x y v m
1: a 1 42 42
2: a 3 42 42
3: a 6 42 42
i.e. DT has a key(DT) = c("x", "y")
, and I want to assign 2 new columns new1
and new2
in one call, similar to the above solution. I do this:
DT[J("a"),c("new1","new2") := list(c(1,2,3),c(3,2,1)),with=FALSE]
but I get that
x y v m new1
1: a 1 42 42 1
2: a 3 42 42 2
3: a 6 42 42 3
4: b 1 4 5 NA
5: b 3 5 5 NA
6: b 6 6 5 NA
7: c 1 7 8 NA
8: c 3 8 8 NA
9: c 6 9 8 NA
i.e. behaviour as expected (assign values to where x==a, NA else), but only for the first column. Is that my mistake or is it a bug?
Notice that without subsetting DT, this works perfectly:
DT[,c("new1","new2") := list(c(1,2,3),c(3,2,1)),with=FALSE]
x y v m new1 new2
1: a 1 42 42 1 3
2: a 3 42 42 2 2
3: a 6 42 42 3 1
4: b 1 4 5 1 3
5: b 3 5 5 2 2
6: b 6 6 5 3 1
7: c 1 7 8 1 3
8: c 3 8 8 2 2
9: c 6 9 8 3 1
Upvotes: 15
Views: 3755
Reputation: 59602
Well spotted: it's a new bug. Please file a bug.report(package="data.table")
.
Thanks.
===
Now fixed in v1.8.3. Bug#2215 and latest NEWS.
Upvotes: 9