2
votes

I have a data.table in R that I would like to modify one of the columns on the fly (but not change the original object) and select a limited number of its columns afterwards by reference. Best I managed is as follows but then my column names are changed, any suggestions?

tmp <- data.table(a = 'X', b = 'Y', d = 1)
tmp[,.(d = d * - 1, .SD), .SDcols = colNames]

1
try c(list(d=d*-1), .SD) in j argument - jangorecki
perfect, it worked. - Burak Oker
You can put that as an answer to your own question. - jangorecki
if you post it as an answer rather than a comment I can accept it as an answer apparently. - Burak Oker

1 Answers

2
votes

try c(list(d=d*-1), .SD) in j argument

j expects a list
.SD is a list

So when adding new column like this you just need to put it into list and combine with c function.