R - 计算具有公共前缀的列之间的比例

问题描述 投票:0回答:1

我有一个包含 37 列的数据框,其中有一个代表性样本,如下

df

df <- structure(list(irm = 201201:201212, trans11 = c(379L, 433L, 468L, 
514L, 559L, 566L, 577L, 622L, 665L, 692L, 738L, 830L), trans12 = c(4L, 
3L, 2L, 3L, 11L, 5L, 3L, 7L, 4L, 4L, 6L, 8L), trans13 = c(5L, 
4L, 9L, 9L, 8L, 9L, 13L, 14L, 12L, 11L, 5L, 8L), trans14 = c(13L, 
3L, 9L, 8L, 6L, 6L, 4L, 10L, 3L, 7L, 5L, 4L), trans15 = c(29L, 
21L, 19L, 15L, 12L, 16L, 21L, 15L, 11L, 15L, 9L, 11L), trans16 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), row.names = c(NA, 
-12L), class = c("data.table", "data.frame"))

数据框有 37 列:

trans11
...
trans16
...
trans61
...
trans66
加上月份的
irm
列。

我想做的是:

  1. 对于

    trans11
    中的每个行/列条目一直到
    trans66
    ,计算该条目相对于具有相同前缀(例如
    trans1
    )的所有其他列的总和的比例。因此,对于此处的示例,第一行将是:第 2 列到第 7 列的
    (0.8813953, 0.009302326, 0.01162791, 0.03023256, 0.06744186, 0)
    (因为我们需要对
    trans11
    ....
    trans16
    求和)

  2. 我该如何对较大 df 中的所有 36 列执行此操作?

有没有办法用

group_by
starts_with
中的
dplyr
来做到这一点?我知道
for
循环可能是可能的,但欢迎任何和所有建议。

谢谢

r dplyr group startswith
1个回答
0
投票
df |>
  mutate(
    row_sum = rowSums(across(starts_with("trans"))),
    across(starts_with("trans"), \(x) x / row_sum, .names = "{.col}_prop")
    )
#        irm trans11 trans12 trans13 trans14 trans15 trans16 row_sum trans11_prop trans12_prop
#  1: 201201     379       4       5      13      29       0     430    0.8813953  0.009302326
#  2: 201202     433       3       4       3      21       0     464    0.9331897  0.006465517
#  3: 201203     468       2       9       9      19       0     507    0.9230769  0.003944773
#  4: 201204     514       3       9       8      15       0     549    0.9362477  0.005464481
#  5: 201205     559      11       8       6      12       0     596    0.9379195  0.018456376
#  6: 201206     566       5       9       6      16       0     602    0.9401993  0.008305648
#  7: 201207     577       3      13       4      21       0     618    0.9336570  0.004854369
#  8: 201208     622       7      14      10      15       0     668    0.9311377  0.010479042
#  9: 201209     665       4      12       3      11       0     695    0.9568345  0.005755396
# 10: 201210     692       4      11       7      15       0     729    0.9492455  0.005486968
# 11: 201211     738       6       5       5       9       0     763    0.9672346  0.007863696
# 12: 201212     830       8       8       4      11       0     861    0.9639954  0.009291521
#     trans13_prop trans14_prop trans15_prop trans16_prop
#  1:  0.011627907  0.030232558   0.06744186            0
#  2:  0.008620690  0.006465517   0.04525862            0
#  3:  0.017751479  0.017751479   0.03747535            0
#  4:  0.016393443  0.014571949   0.02732240            0
#  5:  0.013422819  0.010067114   0.02013423            0
#  6:  0.014950166  0.009966777   0.02657807            0
#  7:  0.021035599  0.006472492   0.03398058            0
#  8:  0.020958084  0.014970060   0.02245509            0
#  9:  0.017266187  0.004316547   0.01582734            0
# 10:  0.015089163  0.009602195   0.02057613            0
# 11:  0.006553080  0.006553080   0.01179554            0
# 12:  0.009291521  0.004645761   0.01277584            0

当然,您可以删除后面的

row_sum
列。

© www.soinside.com 2019 - 2024. All rights reserved.