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

查看:82
本文介绍了在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

我想缩放变量 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 左右是必要的,因此<对code> 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天全站免登陆