我想scale
在我的data.table
. 其中有很多我想要的,scale
所以我想避免按名称指定它们。未缩放的列,我只想按原样返回。这是我希望的工作,但它没有:
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
有什么建议么?