Scaling a subset of data columns. table in R

I would like to scale subset of the columns in my data.table . There are many of them that I would like to scale , so I want to avoid specifying them all by name. Columns that do not scale, I just want to go back as is. Here is what I was hoping to work, but it is not:

 require(data.table) x = data.table(id=1:10, a=sample(1:10,10), b=sample(1:10,10), c=sample(1:10,10)) > dput(x) structure(list(id = 1:10, a = c(1L, 6L, 10L, 7L, 5L, 3L, 2L, 4L, 9L, 8L), b = c(4L, 9L, 5L, 7L, 6L, 1L, 8L, 10L, 3L, 2L), c = c(2L, 7L, 5L, 6L, 4L, 1L, 10L, 9L, 8L, 3L)), .Names = c("id", "a", "b", "c"), row.names = c(NA, -10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x1a85d088>) sx = x[,c(id, lapply(.SD, function(v) as.vector(scale(v)))), .SDcols = colnames(x)[2:4]] Error in eval(expr, envir, enclos) : object 'id' not found 

Any suggestions?

+4
source share
2 answers
 sx = cbind(x[,-(2:4)],data.table(scale(x[,2:4]))) 

I suspect it would be better for your workflow to convert your data.table to a long format.

+3
source

You can also assign by reference in a copy of the data table

 sc <- names(x)[2:4] sx <- copy(x)[ , (sc) := as.data.table(scale(.SD)), .SDcols = sc] 
Scale

returns a matrix, and iirc data.table does not like matrix columns.

Or

 sx <- copy(x)[ , (sc) := lapply(.SD,scale), .SDcols = sc] 

[The brackets around (sc) tell data.table to take the LHS value from the variable value in the call area, not just the column name sc . ]

+9
source

All Articles