将函数应用于data.table中的每个指定列,并按引用进行更新 [英] Apply a function to every specified column in a data.table and update by reference
本文介绍了将函数应用于data.table中的每个指定列,并按引用进行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个data.table,我想用它对某些列执行相同的操作.这些列的名称在字符向量中给出.在这个特定示例中,我想将所有这些列都乘以-1.
I have a data.table with which I'd like to perform the same operation on certain columns. The names of these columns are given in a character vector. In this particular example, I'd like to multiply all of these columns by -1.
一些玩具数据和一个指定相关列的向量:
Some toy data and a vector specifying relevant columns:
library(data.table)
dt <- data.table(a = 1:3, b = 1:3, d = 1:3)
cols <- c("a", "b")
现在我正在这样做,遍历字符向量:
Right now I'm doing it this way, looping over the character vector:
for (col in 1:length(cols)) {
dt[ , eval(parse(text = paste0(cols[col], ":=-1*", cols[col])))]
}
有没有一种方法可以直接执行此操作而无需for循环?
Is there a way to do this directly without the for loop?
推荐答案
这似乎可行:
dt[ , (cols) := lapply(.SD, "*", -1), .SDcols = cols]
结果是
a b d
1: -1 -1 1
2: -2 -2 2
3: -3 -3 3
这里有一些技巧:
- 由于
(cols):=
中有括号,因此将结果分配给cols
中指定的列,而不是分配给一些名为"cols"的新变量. -
.SDcols
告诉调用我们只查看这些列,并允许我们使用.SD
,S
ubset与这些列关联的D
ata的列表. -
lapply(.SD,...)
在.SD
上运行,这是列的列表(如所有data.frames和data.tables).lapply
返回一个列表,所以最后j
看起来像cols:= list(...)
.
- Because there are parentheses in
(cols) :=
, the result is assigned to the columns specified incols
, instead of to some new variable named "cols". .SDcols
tells the call that we're only looking at those columns, and allows us to use.SD
, theS
ubset of theD
ata associated with those columns.lapply(.SD, ...)
operates on.SD
, which is a list of columns (like all data.frames and data.tables).lapply
returns a list, so in the endj
looks likecols := list(...)
.
编辑:这是另一种可能更快的方式,如@Arun所述:
EDIT: Here's another way that is probably faster, as @Arun mentioned:
for (j in cols) set(dt, j = j, value = -dt[[j]])
这篇关于将函数应用于data.table中的每个指定列,并按引用进行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文