[R] data.table - How do I transform a set of column?

Witold E Wolski wewolski at gmail.com
Mon Mar 17 15:25:07 CET 2014


I started to use the data.table to subset, reshape large data.
But how do I transform a set of columns?

# for a data.frame I would do:

df = data.frame(a = c("a","b","c","d"), b = 1:4,c = 1:4)
df[,2:3] = df[,2:3]^2


# but with data.table this somehow similar code produces an error.

dt = data.table(a = c("a","b","c","d"), b = 1:4,c = 1:4)
dt[, 2:3 , with=FALSE ] = x[,2:3,with=FALSE]^2


By the way.

I need the columns selection 2:3 because column 1 in data.table
contains the keys.
With data.frame, matrix I would store the keys as rownames.
and just write:
df = df^2
(easier to read)


Is there syntactic sugar in data.table to be able to say something like:
transform all nonkey columns, replace all nonkey columns?


best regards
Witold









-- 
Witold Eryk Wolski



More information about the R-help mailing list