在r中的data.table缩放

时间:2014-06-17 10:00:08

标签: r data.table scale

       LC  RC TOEIC eua again  class
   1: 490 390   880  90     0 100818
   2: 495 395   890  90     0 100818
   3: 490 330   820  90     0 100818
   4: 495 460   955  96     0 100818
   5: 495 370   865  91     0 100818
  ---                               
1021: 470 400   870  61     0 100770
1022: 260 180   440  48     0 100770
1023: 345 190   535  39     0 100770
1024: 450 295   745  65     0 100770
1025: 395 230   625  79     0 100770

这是名为“analy”的data.table

我想要缩放变量“LC”,“RC”,“TOEIC”,“eua”

我可以像这样扩展

analy[,LC:=scale(LC)]
analy[,RC:=scale(RC)]
analy[,TOEIC:=scale(TOEIC)]
analy[,eua:=scale(eua)]

但是,我想知道如何一次扩展变量

3 个答案:

答案 0 :(得分:10)

analy[ , c("LC", "RC", "TOEIC", "eua") := lapply(list(LC, RC, TOEIC, eua), scale)] 

更方便的做法是(正如@David在评论中提到的那样):

cols <- c("LC", "RC", "TOEIC", "eua")
analy[, (cols) := lapply(.SD, scale), .SDcols=cols]

请注意,(周围的cols是必需的,以便评估cols以获取列名,然后通过引用修改它们。这样我们仍然可以继续:DT[ ,col := val]

答案 1 :(得分:2)

这与使用此处发布的.SD Computing inter-value differences in data.table columns (with .SD) in R的data.table中的预处理列进行更一般性的问题有关。

以下是您的问题的答案,以及如果您错误地使用scale()功能将会得到的结果:

DT <- data.table(K=c(rep(1,5),rep(2,5)), X=(1:10)^2, Y=2^(1:10))
cols <- 2:3;  cols.d0 = paste0("d0.", names(DT)[cols])

# Correct and incorrect use of scale() with data.table

# Works for one column.
DT[, d0_Y:= scale(Y), keyby=K][]

# RUNS BUT GIVES WRONG RESULT! ==> returns 1:20 data.table!
DT[, scale(.SD), keyby=K, .SDcols=cols][]

# RUNS WITH WARNING AND GIVES WRONG RESULT! - d0.X is computed correctly, by d0.Y not (compare to d0_Y) !
DT[, (cols.d0) := scale(.SD), keyby=K, .SDcols=cols][]
>     K   X    Y       d0_Y        d0.X        d0.Y
   1: 1   1    2 -0.8525736 -1.03417538 -1.03417538
   ...

# DOESN'T RUN ! - ERROR
DT[, (cols.d0) := lapply(.SD, scale), keyby=K, .SDcols=cols][]

# WORKS CORRECTLY AS DESIRED !
DT[, (cols.d0) := lapply(.SD, function(x) as.vector(scale(x))), keyby=K, .SDcols=cols][] 

答案 2 :(得分:0)

以下答案有效。

SearchOption.AllDirectories

由于library(data.table) # Data dt <- iris setDT(dt) # columns to apply the scale function cols <- colnames(dt)[-5] # standerdize dt[, (cols) := lapply(.SD, function(x) as.vector(scale(x))), by = Species, .SDcols = cols] 返回矩阵,因此scale用于转换为向量。