r多列减法 [英] r multiple column subtraction
问题描述
我的数据集的简化版本如下所示。
The reduced version of my dataset is as shown below.
Z_dog1_mu1 Z_dog2_mu1 Z_dog3_mu1 Z_cat1_mu1 Z_cat2_mu1 Z_cat3_mu1
0.0000 0.0000 0.0001 0.0005 0.0043 0.0045
0.0039 -0.0016 -0.0102 -0.0009 0.0421 -0.0139
-0.0380 -0.0733 0.0196 0.0261 0.0628 0.0463
-0.1036 0.0784 -0.0529 0.1053 -0.0511 -0.0138
我正在尝试从cat *列中减去dog *列
I am trying to substract the dog* columns from the cat* columns like this
df$diff1 <- df$Z_dog1_mu1 - df$Z_cat1_mu1
df$diff2 <- df$Z_dog2_mu1 - df$Z_cat2_mu1
df$diff3 <- df$Z_dog3_mu1 - df$Z_cat3_mu1
如何在不手动减去每一列的情况下更高效,更快速地执行此操作,如上所述。我大约有100条狗栏(Z_dog1_mu1 ... Z_dog100_mu1)
和100条猫栏(Z_cat1_mu1 ... Z_cat100_mu1)
/。任何建议都非常有用。
How can I do this more efficiently and faster without manually subtracting each column as shown above. I have around 100 dog columns (Z_dog1_mu1...Z_dog100_mu1)
and 100 cat columns(Z_cat1_mu1...Z_cat100_mu1)
/. Any advise is much appriciated.
推荐答案
我们分别对狗列和猫列进行子集化,然后进行减法
We subset the 'dog' columns, and 'cat' columns separately and then do the subtraction
nmdog <- grep("^Z_dog\\d+_mu", names(df))
nmcat <- grep("^Z_cat\\d+_mu", names(df))
df[paste0("diff", seq_along(nmdog))] <- df[nmdog] - df[nmcat]
df
# Z_dog1_mu1 Z_dog2_mu1 Z_dog3_mu1 Z_cat1_mu1 Z_cat2_mu1 Z_cat3_mu1 diff1 diff2 diff3
#1 0.0000 0.0000 0.0001 0.0005 0.0043 0.0045 -0.0005 -0.0043 -0.0044
#2 0.0039 -0.0016 -0.0102 -0.0009 0.0421 -0.0139 0.0048 -0.0437 0.0037
#3 -0.0380 -0.0733 0.0196 0.0261 0.0628 0.0463 -0.0641 -0.1361 -0.0267
#4 -0.1036 0.0784 -0.0529 0.1053 -0.0511 -0.0138 -0.2089 0.1295 -0.0391
注意:如示例所示,我们假设 dog列序列与 cat列序列相对应,即1:10 0
NOTE: As showed in the example, we assume that the 'dog' column sequence corresponds to the 'cat' column sequence i.e. 1:100
df <- structure(list(Z_dog1_mu1 = c(0, 0.0039, -0.038, -0.1036), Z_dog2_mu1 = c(0,
-0.0016, -0.0733, 0.0784), Z_dog3_mu1 = c(1e-04, -0.0102, 0.0196,
-0.0529), Z_cat1_mu1 = c(5e-04, -9e-04, 0.0261, 0.1053), Z_cat2_mu1 = c(0.0043,
0.0421, 0.0628, -0.0511), Z_cat3_mu1 = c(0.0045, -0.0139, 0.0463,
-0.0138)), class = "data.frame", row.names = c(NA, -4L))
这篇关于r多列减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!