This comes out of this attempted (Japanese) SO answer of mine.
At essence, I wanted to to two things within []: add some new columns, and delete a column.
MRE:
DT = data.table(id = c(1, 1, 2, 2), a = 1:4, b = 5:8)
DT[ , c("c", "a") := .(a + 1, NULL), by = id]
Gives error:
Error in [.data.table(DT, , :=(c("c", "a"), .(a + 1, NULL)), by = id) :
Type of RHS ('NULL') must match LHS ('integer'). To check and coerce would impact performance too much for the fastest cases. Either change the type of the target column, or coerce the RHS of := yourself (e.g. by using 1L instead of 1)
However, this approach usually works:
DT[ , c("c", "a") := .(a + 1, NULL)][]
# id b c
#1: 1 5 2
#2: 1 6 3
#3: 2 7 4
#4: 2 8 5
Can't see why we should be able to do it by, and in fact it works if deleting is all we're trying to do:
DT[ , a := NULL, by = id][]
# id a b
#1: 1 1 5
#2: 1 2 6
#3: 2 3 7
#4: 2 4 8
So something's going wrong when we are trying to both delete a column in a by operation and create some other columns at the same time. (i.e., the operation also succeeds if we try and delete two columns: DT[ , c("a", "b") := NULL, by = id]; and it also fails if the created column doesn't depend on the column to be deleted: DT[ , c("c", "a") := .(b + 1, NULL), by = id])
This comes out of this attempted (Japanese) SO answer of mine.
At essence, I wanted to to two things within
[]: add some new columns, and delete a column.MRE:
Gives error:
However, this approach usually works:
Can't see why we should be able to do it
by, and in fact it works if deleting is all we're trying to do:So something's going wrong when we are trying to both delete a column in a
byoperation and create some other columns at the same time. (i.e., the operation also succeeds if we try and delete two columns:DT[ , c("a", "b") := NULL, by = id]; and it also fails if the created column doesn't depend on the column to be deleted:DT[ , c("c", "a") := .(b + 1, NULL), by = id])