You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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])
The text was updated successfully, but these errors were encountered:
Delete column by group sounds like a strange operation to me. Since operations are by reference, there's "no" cost to just splitting this into two [ queries.
To close this issue, we might try and improve the error message.
Looks like the message has already improved in the interim:
DT[ , c("c", "a") := .(a + 1, NULL), by = id]
Error in `[.data.table`(DT, , `:=`(c("c", "a"), .(a + 1, NULL)), by = id) :
RHS of := is NULL during grouped assignment, but it's not possible to delete parts of a column.
This happened in #3310 but I don't see a specific regression test here. Let's add one to close this issue.
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
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]
)The text was updated successfully, but these errors were encountered: