15

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
Community
  • 1
  • 1
Florian Oswald
  • 5,054
  • 5
  • 30
  • 38
  • 1
    I'd expect the RHS to recycle when doing `DT[,c("new1","new2") := list(c(1,2,3),c(3,2,1)),with=FALSE]`, in your example it repeats the last vector. – Thell Aug 21 '12 at 13:07
  • 1
    of course. as I said `DT[,c("new1","new2") := list(c(1,2,3),c(3,2,1)),with=FALSE]` works perfectly fine. it recycles each list entry, not the last vector. it's when subsetting DT that I'm a bit confused. – Florian Oswald Aug 21 '12 at 13:11

1 Answers1

9

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.

Matt Dowle
  • 58,872
  • 22
  • 166
  • 224