R数据表。多列转换的名称 [英] R data.table multi column coversion by names

查看:92
本文介绍了R数据表。多列转换的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让DT为数据表:

  DT <-data.table(V1 = factor ,
V2 = factor(1:10),
...
V9 = factor(1:10),)

有这样一个更好/更简单的方法来做多列转换:

  DT [,`:=`(
Vn1 = as.numeric(V1),
Vn2 = as.numeric(V2),
Vn3 = as.numeric
Vn4 = as.numeric(V4),
Vn5 = as.numeric(V5),
Vn6 = as.numeric(V6),
Vn7 = as.numeric ),
Vn8 = as.numeric(V8),
Vn9 = as.numeric(V9)
)]
>

列名是完全任意的。

解决方案

可能在中为循环运行设置



修改所需的列(您可以改为使用名称(DT)选择所有名称)

  cols <-c(V1,V2,V3)


$ b b

然后只需运行循环

  for(j in cols)set(DT,i = NULL,j = j ,value = as.numeric(DT [[j]]))

(注意 cols 左侧的圆括号,用于计算变量)

  ##如果你在DT中选择了所有的名字,你不需要指定`.SDcols`参数
DT [,(cols):= lapply(.SD,as.numeric) .SDcols = cols]

两者对于大数据集都应该是有效的。您可以阅读更多关于 data.table 的基础知识此处






虽然谨慎 c> factor s至数字类,请参阅此处了解更多详情


Let DT be a data.table:

DT<-data.table(V1=factor(1:10),
           V2=factor(1:10),
           ...
           V9=factor(1:10),)

Is there a better/simpler method to do multicolumn factor conversion like this:

DT[,`:=`(
  Vn1=as.numeric(V1),
  Vn2=as.numeric(V2),
  Vn3=as.numeric(V3),
  Vn4=as.numeric(V4),
  Vn5=as.numeric(V5),
  Vn6=as.numeric(V6),
  Vn7=as.numeric(V7),
  Vn8=as.numeric(V8),
  Vn9=as.numeric(V9)
)]

Column names are totally arbitrary.

解决方案

Yes, the most efficient would be probably to run set in a for loop

Set the desired columns to modify (you can chose all the names too using names(DT) instead)

cols <- c("V1", "V2", "V3") 

Then just run the loop

for (j in cols) set(DT, i = NULL, j = j, value = as.numeric(DT[[j]]))

Or a bit less efficient but more readable way would be just (note the parenthesis around cols which evaluating the variable)

## if you chose all the names in DT, you don't need to specify the `.SDcols` parameter
DT[, (cols) := lapply(.SD, as.numeric), .SDcols = cols] 

Both should be efficient even for a big data set. You can read some more about data.table basics here


Though beware of converting factors to numeric classes in such a way, see here for more details

这篇关于R数据表。多列转换的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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