我想scale
我的data.table
中的一个列的子集。有很多这些,我想scale
,所以我想避免指定他们所有的名字。没有被缩放的列,我只想返回原样。这是我希望能工作,但它不:缩放R中的一列data.table的子集
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
有什么建议吗?
真棒,谢谢!实际上这是工作过程的结束。在此之后的步骤进行回归,并且结束它。 – Alex 2013-03-17 18:51:32
这是我发现的唯一可以处理'by ='子句的解决方案:)。从'ddply'惊人的加速。 – user17130 2014-10-22 00:13:19