通过列索引而不是名称将函数应用于data.table列的子集 [英] Apply a function to a subset of data.table columns, by column-indices instead of name

查看:73
本文介绍了通过列索引而不是名称将函数应用于data.table列的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个函数应用于大型data.table中的一组列,而不必单独引用每个列.

I'm trying to apply a function to a group of columns in a large data.table without referring to each one individually.

a <- data.table(
  a=as.character(rnorm(5)),
  b=as.character(rnorm(5)),
  c=as.character(rnorm(5)),
  d=as.character(rnorm(5))
)
b <- c('a','b','c','d')

具有上述MWE,

a[,b=as.numeric(b),with=F]

可以,但是:

a[,b[2:3]:=data.table(as.numeric(b[2:3])),with=F]

不起作用.将as.numeric函数仅应用于a的第2列和第3列而不单独引用它们的正确方法是什么.

doesn't work. What is the correct way to apply the as.numeric function to just columns 2 and 3 of a without referring to them individually.

(在实际数据集中有数十列,因此不切实际)

(In the actual data set there are tens of columns so it would be impractical)

推荐答案

惯用的方法是使用.SD.SDcols

您可以通过包装在()

a[, (b) := lapply(.SD, as.numeric), .SDcols = b]

对于第2列:3

a[, 2:3 := lapply(.SD, as.numeric), .SDcols = 2:3]

mysubset <- 2:3
a[, (mysubset) := lapply(.SD, as.numeric), .SDcols = mysubset]

这篇关于通过列索引而不是名称将函数应用于data.table列的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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