如何将相同的函数应用于data.table中的每个指定列 [英] How to apply same function to every specified column in a data.table
问题描述
我有一个data.table,我想对某些列执行相同的操作。这些列的名称在字符向量中给出。在这个特定的例子中,我想把所有这些列乘以-1。
一些玩具数据和一个指定相关列的向量:
library(data.table)
dt< - data.table(a = 1:3,b = 1:3,d = 1:3)
cols < - c(a,b)
现在我这样做,循环的字符向量:
for(col in 1:length )){
dt [,eval(parse(text = paste0(cols [col],:= - 1 *,cols [col]))]
}
有没有办法直接做这个没有for循环?
解决方案这似乎工作:
dt [,(cols):= lapply .SD,*,-1),.SDcols = cols]
p>
abd
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 ..)
。
EDIT ,如@Arun提到的:
for(j in cols)set(dt,j = j,value = j]])
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])))]
}
Is there a way to do this directly without the for loop?
解决方案 This seems to work:
dt[ , (cols) := lapply(.SD, "*", -1), .SDcols = cols]
The result is
a b d
1: -1 -1 1
2: -2 -2 2
3: -3 -3 3
There are a few tricks here:
- Because there are parentheses in
(cols) :=
, the result is assigned to the columns specified in cols
, 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
, the S
ubset of the D
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 end j
looks like cols := list(...)
.
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屋!