r行乘法和行总和

时间:2018-10-08 04:56:57

标签: r dplyr apply

我正在处理一个大型数据集,该数据集的压缩版本如下所示

     Col1     Z1         Z2         Z3         Z4           Z5         Z6     

     0.077    1.6163062  0.7573661 -1.1250619 -1.046367579 -0.9179156 -0.1372472  0.1200780 -0.1669360 -0.8295464  0.6470479
     0.01    -0.6611817 -0.4043414 -2.0809741  1.129659352  0.4477784  0.1425769 -1.4063330 -0.2119068  0.1621555  0.6139109
     0.033   -0.7142192 -0.1317318 -0.3298212  0.651356337 -0.8588449  1.5542608  0.1376427 -1.0568366 -0.7058332  0.2803810
     0.05    -1.0913298 -0.4996984 -0.6667251 -0.022393207  1.2802708 -0.3077195 -0.8905316  1.5384766 -0.8538084 -2.1926031
     0.23    -0.1383636  0.3953499  0.6674759 -1.517436558  1.0223679  1.2748456  0.2315481 -0.1999777 -1.2599197 -1.6293457
     0.78     0.6727714  0.5522693  0.8231503 -0.006216825  1.3181397 -0.3797453  0.9328081 -0.8693609 -0.7529270  2.1232194

我正在寻找的是有效地将Z columnsCol1

相乘

我不想做Col1 * Z1,Col * Z2,Col1 * Z3等。因为我有100列以Z开头的列,所以手动执行此操作效率低下或不可能。

是否可以有效地将Z列中的每个元素与Col1相乘?在此先感谢您的任何建议或帮助。

1 个答案:

答案 0 :(得分:0)

您可以使用mutate_at并指定要修改的列

library(dplyr)

df %>% 
  mutate_at(.vars = vars(starts_with("Z")), .funs = funs(. * Col1))
#>    Col1           Z1           Z2          Z3           Z4           Z5
#> 1 0.077  0.124455577  0.058317190 -0.08662977 -0.080570304 -0.070679501
#> 2 0.010 -0.006611817 -0.004043414 -0.02080974  0.011296594  0.004477784
#> 3 0.033 -0.023569234 -0.004347149 -0.01088410  0.021494759 -0.028341882
#> 4 0.050 -0.054566490 -0.024984920 -0.03333626 -0.001119660  0.064013540
#> 5 0.230 -0.031823628  0.090930477  0.15351946 -0.349010408  0.235144617
#> 6 0.780  0.524761692  0.430770054  0.64205723 -0.004849124  1.028148966
#>             Z6           Z7           Z8           Z9          Z10
#> 1 -0.010568034  0.009246006 -0.012854072 -0.063875073  0.049822688
#> 2  0.001425769 -0.014063330 -0.002119068  0.001621555  0.006139109
#> 3  0.051290606  0.004542209 -0.034875608 -0.023292496  0.009252573
#> 4 -0.015385975 -0.044526580  0.076923830 -0.042690420 -0.109630155
#> 5  0.293214488  0.053256063 -0.045994871 -0.289781531 -0.374749511
#> 6 -0.296201334  0.727590318 -0.678101502 -0.587283060  1.656111132


df %>% 
  mutate_at(.vars = vars(starts_with("Z")), .funs = funs(mul = . * Col1))
#>    Col1         Z1         Z2         Z3           Z4         Z5
#> 1 0.077  1.6163062  0.7573661 -1.1250619 -1.046367579 -0.9179156
#> 2 0.010 -0.6611817 -0.4043414 -2.0809741  1.129659352  0.4477784
#> 3 0.033 -0.7142192 -0.1317318 -0.3298212  0.651356337 -0.8588449
#> 4 0.050 -1.0913298 -0.4996984 -0.6667251 -0.022393207  1.2802708
#> 5 0.230 -0.1383636  0.3953499  0.6674759 -1.517436558  1.0223679
#> 6 0.780  0.6727714  0.5522693  0.8231503 -0.006216825  1.3181397
#>           Z6         Z7         Z8         Z9        Z10       Z1_mul
#> 1 -0.1372472  0.1200780 -0.1669360 -0.8295464  0.6470479  0.124455577
#> 2  0.1425769 -1.4063330 -0.2119068  0.1621555  0.6139109 -0.006611817
#> 3  1.5542608  0.1376427 -1.0568366 -0.7058332  0.2803810 -0.023569234
#> 4 -0.3077195 -0.8905316  1.5384766 -0.8538084 -2.1926031 -0.054566490
#> 5  1.2748456  0.2315481 -0.1999777 -1.2599197 -1.6293457 -0.031823628
#> 6 -0.3797453  0.9328081 -0.8693609 -0.7529270  2.1232194  0.524761692
#>         Z2_mul      Z3_mul       Z4_mul       Z5_mul       Z6_mul
#> 1  0.058317190 -0.08662977 -0.080570304 -0.070679501 -0.010568034
#> 2 -0.004043414 -0.02080974  0.011296594  0.004477784  0.001425769
#> 3 -0.004347149 -0.01088410  0.021494759 -0.028341882  0.051290606
#> 4 -0.024984920 -0.03333626 -0.001119660  0.064013540 -0.015385975
#> 5  0.090930477  0.15351946 -0.349010408  0.235144617  0.293214488
#> 6  0.430770054  0.64205723 -0.004849124  1.028148966 -0.296201334
#>         Z7_mul       Z8_mul       Z9_mul      Z10_mul
#> 1  0.009246006 -0.012854072 -0.063875073  0.049822688
#> 2 -0.014063330 -0.002119068  0.001621555  0.006139109
#> 3  0.004542209 -0.034875608 -0.023292496  0.009252573
#> 4 -0.044526580  0.076923830 -0.042690420 -0.109630155
#> 5  0.053256063 -0.045994871 -0.289781531 -0.374749511
#> 6  0.727590318 -0.678101502 -0.587283060  1.656111132

如果要更新的表很大,使用data.table可能会更快

library(data.table)   

dt <- data.table(df)
cols <- names(dt)[-1]
dt[, (cols) := lapply(.SD, function(x) x*Col1), .SDcols = cols]
dt
#>     Col1           Z1           Z2          Z3           Z4           Z5
#> 1: 0.077  0.124455577  0.058317190 -0.08662977 -0.080570304 -0.070679501
#> 2: 0.010 -0.006611817 -0.004043414 -0.02080974  0.011296594  0.004477784
#> 3: 0.033 -0.023569234 -0.004347149 -0.01088410  0.021494759 -0.028341882
#> 4: 0.050 -0.054566490 -0.024984920 -0.03333626 -0.001119660  0.064013540
#> 5: 0.230 -0.031823628  0.090930477  0.15351946 -0.349010408  0.235144617
#> 6: 0.780  0.524761692  0.430770054  0.64205723 -0.004849124  1.028148966
#>              Z6           Z7           Z8           Z9          Z10
#> 1: -0.010568034  0.009246006 -0.012854072 -0.063875073  0.049822688
#> 2:  0.001425769 -0.014063330 -0.002119068  0.001621555  0.006139109
#> 3:  0.051290606  0.004542209 -0.034875608 -0.023292496  0.009252573
#> 4: -0.015385975 -0.044526580  0.076923830 -0.042690420 -0.109630155
#> 5:  0.293214488  0.053256063 -0.045994871 -0.289781531 -0.374749511
#> 6: -0.296201334  0.727590318 -0.678101502 -0.587283060  1.656111132

reprex package(v0.2.1.9000)于2018-10-07创建