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?
One way to solve your problem. Note that you can use a data.frame (and therefore
.SD
) followed by a multiplication operator (*
). In the following example, each column in.SD
will be multiplied by1e-2 * c
.