I am trying to accomplish the following
library(data.table)
DT <- data.table(
ID=c("b", "b", "b", "a", "a", "c"),
a=1:6,
b=7:12,
c=13:18
)
I am trying to accomplish the following
oldcols <- c("a", "b")
newcols <- c("a_1", "b_1")
DT[, c(..newcols) := DT[, ..ldcols]*1e-2*DT[, "c"]]
this is not working. what am I doing wrong?
In
data.table, the LHS of the:=operator must be a character vector. Since you want to use a character vector that is bound to a name in the global environment, you have to enclose the name in(). The RHS of the:=operator must be a list, e.g, the return value oflapply().lapply()applies the anonymous function\(x) x * 1e-2 * cto each element of.SD, which is defined asoldcolsin the.SDcolsargument. Note this anonymous function could have also been written asfunction(x) x * 1e-2 * c.