基于列名称模式的成对差异 [英] Multiple pairwise differences based on column name patterns
问题描述
我有一个 data.table
,dt:
dt
Id v1 v2 v3 x1 x2 x3
1 7 1 3 5 6 8
2 1 3 5 6 8 5
3 3 5 6 8 5 1
v1,v2,v3和x1,x2,x3是数字变量
v1, v2, v3 and x1, x2, x3 are numeric variables
我想要从 v列中减去 x列,即计算差异 v1-x1
, v2-x2
等。在我的真实数据中,我可能有100个这样的变量对。
I want to subtract the 'x' columns from the 'v' columns, i.e. calculate the differences v1 - x1
, v2 - x2
, etc. In my real data I may have 100s of such pair of variables.
所需的输出:
dt
Id v1 v2 v3 x1 x2 x3 diff1 diff2 diff3
1 7 1 3 5 6 8 -2 -4 -3
2 1 3 5 6 8 5 -5 -5 0
3 3 5 6 8 5 1 -3 0 5
我尝试了以下操作:
I've tried out the following:
newnames <- paste0("diff", 1:3)
v <- paste0("v", 1:3)
x <- paste0("x", 1:3)
dt[ , c(newnames) := get(v) - get(x)]
Howeve r,这将导致3个相同的列,都包含差异 v1-x1
。
However, this results in 3 identical columns all containing the difference v1 - x1
.
我知道可能的解决方案是
I am aware that a possible solution is something like
dt[ , .(v1 - x1, v2 - x2, v3 - x3)]
但是,如果我必须输入100个不像v1和x1一样简单的名称,这是一个很长的代码,可能会出现很多键入错误。
However this is quite a long code with possible many typing errors if I have to put in 100 names not as simple as v1 and x1.
希望您能为我提供帮助。
I hope you can help me.
推荐答案
您可以按列是否包含 x
,然后取所得数据表的差。
You could split by whether the column contains x
and then take the difference of the resulting data tables.
new_cols <-
do.call('-', split.default(dt[,-1], grepl('x', names(dt)[-1])))
dt[, paste0('diff', seq_along(new_cols)) := new_cols]
dt
# Id v1 v2 v3 x1 x2 x3 diff1 diff2 diff3
# 1: 1 7 1 3 5 6 8 2 -5 -5
# 2: 2 1 3 5 6 8 5 -5 -5 0
# 3: 3 3 5 6 8 5 1 -5 0 5
或使用与您可以执行的问题中的代码段相似的逻辑
Or using similar logic to the code snippet in the question you could do
newnames <- paste0("diff",1:3)
v <- paste0("v",1:3)
x <- paste0("x",1:3)
dt[, (newnames) := Map('-', mget(v), mget(x))]
dt
# Id v1 v2 v3 x1 x2 x3 diff1 diff2 diff3
# 1: 1 7 1 3 5 6 8 2 -5 -5
# 2: 2 1 3 5 6 8 5 -5 -5 0
# 3: 3 3 5 6 8 5 1 -5 0 5
这篇关于基于列名称模式的成对差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!