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

查看:19
本文介绍了通过列索引而不是名称将函数应用于 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,这个:

with the MWE above, this:

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天全站免登陆