【问题标题】:Best way to do row operations in R using Pivot_wider使用 Pivot_wider 在 R 中执行行操作的最佳方法
【发布时间】:2022-01-05 20:36:43
【问题描述】:

以下是示例数据。目标是通过 indcode 和所有权将行合并为一个。区域 029 和区域 031 需要合并成一个新的行,039900。目前,每个区域有四行。希望通过 indcode 创建另外 4 行,它们是两个区域的总和。我昨天问了一个非常相似的问题,但正在寻找一个涉及更多以 dplyr 为中心的版本的解决方案。我的第一次尝试是在底部

library(dplyr)
library(data.table)


areax <- c(029,029,029,029,031,031,031,031)
indcodex <- c(1011,1012,1011,1012,1011,1012,1011,1012)
time1 <- c(100,150,102,152,104,154,108,158)
time2 <- c(101,151,103,153,105,155,109,162)
ownership <- c(50,50,30,30,50,50,30,30)

test2 <- data.frame(areax,indcodex,time1,time2,ownership)

想要的结果

   areax        indcodex      time1     time2    ownership
     029           1011         100        101       50
     029           1012         150        151       50
     029           1011         102        103       30
     029           1021         152        153       30
     031           1011         104        105       50
     031           1012         154        155       50
     031           1011         108        109       30
     031           1021         158        162       30
     039900        1011         204        206       50
     039900        1012         304        306       50
     039900        1011         210        212       30
     039900        1012         310        315       30
   

   test3 <- test2 %>%
   tidyr::pivot_wider(names_from = areax, values_from = time1:time2)

   test3$newarea <- (time1_29 + time1_31)

【问题讨论】:

    标签: r dplyr sum pivot row


    【解决方案1】:
    test2 %>%
      add_row(
        areax = 039900,
        group_by(.,indcodex, ownership)%>%
          summarise(across(-areax, sum), .groups = 'drop'))
    
      areax indcodex time1 time2 ownership
    1     29     1011   100   101        50
    2     29     1012   150   151        50
    3     29     1011   102   103        30
    4     29     1012   152   153        30
    5     31     1011   104   105        50
    6     31     1012   154   155        50
    7     31     1011   108   109        30
    8     31     1012   158   162        30
    9  39900     1011   210   212        30
    10 39900     1011   204   206        50
    11 39900     1012   310   315        30
    12 39900     1012   304   306        50
    

    在基础 R 中:

     rbind(test2, cbind(areax = 39900,
          aggregate(.~indcodex + ownership, test2[-1], sum)))
    
       areax indcodex time1 time2 ownership
    1     29     1011   100   101        50
    2     29     1012   150   151        50
    3     29     1011   102   103        30
    4     29     1012   152   153        30
    5     31     1011   104   105        50
    6     31     1012   154   155        50
    7     31     1011   108   109        30
    8     31     1012   158   162        30
    9  39900     1011   210   212        30
    10 39900     1012   310   315        30
    11 39900     1011   204   206        50
    12 39900     1012   304   306        50
    

    【讨论】:

    • 如果可能的话,你能解释一下这部分背后的逻辑,“group_by(.”,”。为什么在 indcodex 之前的“.”,所有权?
    • @TimWilcox . 是包含管道输入数据的对象。
    猜你喜欢
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 2022-01-12
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多