在df中创建所选列的组合

时间:2018-04-30 18:45:16

标签: r dplyr

让我们从这个数据框开始。对于分别以varchar(1000)开头的列,我们可以将它们称为tempppt列。

t and p

我的最终目标是创建一个df <- data.frame(id = 1:4, area = rnorm(4, 123), t_p25 = rnorm(4, 2), t_p30 = rnorm(4, 0.5), t_m25 = rnorm(4, 3), p1 = rnorm(4, 7), p_1 = rnorm(4, 10), p2 = rnorm(4, 3), p_2 = rnorm(4, -1)) 列,其计算结果为W

  
    

例如:
    W = temp * ppt * area * {other columns as well}
    W_t_p25_p1 = t_p25 * p1 * area
    W_t_p25_p_1 = t_p25 * p_1 * area     W_t_m25_p1 = t_m25 * p1 * area等等

  

这就是我需要它们的方式::

W_t_m25_p_1  = t_m25 * p_1 * area

手动操作会很繁琐。我试图用id area W_t_p25_p1 W_p_25_p_1 W_p_25_p2 W_p_25_p_2 W_t_p30_p1 W_t_p30_p_1 W_p_25_p2 W_p_25_p_2 ... and so on # ## # # # # # # # # and so on 解决它,但.vars是限制。如果只能做到这一点::

mutate_at

有没有更简单的方法来做这样的事情?

1 个答案:

答案 0 :(得分:2)

这是我能想到的一个解决方案(您可以根据df中存在多少小写和大写字母来调整索引:

egrid <- expand.grid(names(df)[3:5], names(df)[6:9])
result <- as.data.frame(apply(egrid, 1, function(x) df$area * df[[x[1]]] * df[[x[2]]]))

colnames(result) <- paste0('W_', egrid$Var1, egrid$Var2)
result <- cbind(df[, 1:2], result)
result

  id     area     W_aA       W_bA     W_cA     W_aB       W_bB     W_cB      W_aC       W_bC     W_cC       W_aD        W_bD        W_cD
1  1 122.6077 2004.342 1235.42437 3253.378 2882.802 1776.88439 4679.264  795.9592 490.608589 1291.973 -976.96029 -602.172981 -1585.76799
2  2 126.1399 1713.637  -62.30608 2147.569 2483.034  -90.28058 3111.795 1291.2969 -46.950231 1618.283  221.76815   -8.063262   277.92486
3  3 120.9471 1282.721   -9.47124 4080.824 1790.517  -13.22066 5696.317  682.9681  -5.042838 2172.781 -159.77938    1.179764  -508.31898
4  4 121.7203 1634.049  392.82541 2938.293 2262.283  543.85269 4067.961  833.1827 200.297098 1498.201   13.87204    3.334838    24.94424