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
this is data.table named "analy"
i want scale variables "LC","RC","TOEIC","eua"
i can scale like this
analy[,LC:=scale(LC)]
analy[,RC:=scale(RC)]
analy[,TOEIC:=scale(TOEIC)]
analy[,eua:=scale(eua)]
but, i wanna know how to scale that variables at once
analy[ , c("LC", "RC", "TOEIC", "eua") := lapply(list(LC, RC, TOEIC, eua), scale)]
A little more convenient way of doing it would be (as @David mentions under comment):
cols <- c("LC", "RC", "TOEIC", "eua")
analy[, (cols) := lapply(.SD, scale), .SDcols=cols]
Note the (
around cols
is necessary so that cols
is evaluated to get the column names, and then modify them by reference. This is so that we can still continue doing: DT[ ,col := val]
.
This is related to a more general question on pre-processing columns in data.table using .SD
posted here: Computing inter-value differences in data.table columns (with .SD) in R.
Here's the answer to your question, and what you will get if you use scale()
function incorrectly:
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][]