在 r 中的 data.table 中缩放 [英] scale in data.table in r

查看:15
本文介绍了在 r 中的 data.table 中缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

       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

这个data.table被命名为analy"

This data.table is named "analy"

我想缩放变量LC"、RC"、TOEIC"、eua".我可以按如下方式缩放

I want to scale the variables "LC","RC","TOEIC","eua". I can scale as below

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

但是,我想知道如何一次缩放变量.

but, I want to know how to scale the variables at once.

推荐答案

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

<小时>

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


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]

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

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].

这篇关于在 r 中的 data.table 中缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆