r multiple column subtraction

佐手、 提交于 2020-06-27 23:49:48

问题


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

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  

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.


回答1:


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

NOTE: As showed in the example, we assume that the 'dog' column sequence corresponds to the 'cat' column sequence i.e. 1:100

data

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))


来源:https://stackoverflow.com/questions/52731483/r-multiple-column-subtraction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!