【问题标题】:ID wise sum of a value值的 ID 明智总和
【发布时间】:2023-03-30 08:02:01
【问题描述】:

我有一个这样的日常数据数据框

date    firms value  ID
6/4/2007    A   16    1
6/5/2007    A   18    1
6/20/2007   A   22    2
6/29/2007   A   25    2
6/21/2007   A   12    3
6/4/2007    B   14    1
6/5/2007    B   19    2
6/20/2007   B   17    2
6/29/2007   B   12    1
6/21/2007   B   10    3

现在我想获得该值的每一天的 ID 明智总和。由于是面板数据,因此总和值将在重复日期重复。期望值输出是这样的:

date    firms   value   ID        ID 1 Sum    ID 2 Sum      ID 3 Sum 
6/4/2007    A    16     1             30           0              0
6/5/2007    A    18     1             18          19              0
6/20/2007   A    22     2              0          39              0
6/29/2007   A    25     2             12          25              0
6/21/2007   A    12     3              0           0              22
6/4/2007    B    14     1             30           0              0
6/5/2007    B    19     2             18          19              0
6/20/2007   B    17     2              0          39              0
6/29/2007   B    12     1             12          25              0
6/21/2007   B    10     3              0          0               22

请在这方面帮助我。我无法从互联网上找到代码。

【问题讨论】:

    标签: r dplyr sum tidyr


    【解决方案1】:

    您可以将数据从长格式转换为宽格式,然后使用summarise_ifmutate_if 来获得所需的输出

    要修改结果列的名称,请参阅answer

    library(dplyr)
    library(tidyr)
    
    df <- read.table(text = txt, header = TRUE, stringsAsFactors = FALSE)
    
    df_wide <- df %>% 
      mutate(date = as.Date(date, '%m/%d/%Y')) %>% 
      mutate(rowid = row_number()) %>% 
      spread(ID, value) %>% 
      select(-rowid)
    
    df_wide %>% 
      group_by(date) %>% 
      summarise_if(is.numeric, funs(sum(., na.rm = TRUE)))
    #> # A tibble: 5 x 4
    #>   date         `1`   `2`   `3`
    #>   <date>     <int> <int> <int>
    #> 1 2007-06-04    30     0     0
    #> 2 2007-06-05    18    19     0
    #> 3 2007-06-20     0    39     0
    #> 4 2007-06-21     0     0    22
    #> 5 2007-06-29    12    25     0
    
    df_wide %>% 
      group_by(date) %>% 
      mutate_if(is.numeric, funs(sum(., na.rm = TRUE))) %>% 
      arrange(firms)
    #> # A tibble: 10 x 5
    #> # Groups:   date [5]
    #>    date       firms   `1`   `2`   `3`
    #>    <date>     <chr> <int> <int> <int>
    #>  1 2007-06-04 A        30     0     0
    #>  2 2007-06-05 A        18    19     0
    #>  3 2007-06-20 A         0    39     0
    #>  4 2007-06-21 A         0     0    22
    #>  5 2007-06-29 A        12    25     0
    #>  6 2007-06-04 B        30     0     0
    #>  7 2007-06-05 B        18    19     0
    #>  8 2007-06-20 B         0    39     0
    #>  9 2007-06-21 B         0     0    22
    #> 10 2007-06-29 B        12    25     0
    

    reprex package (v0.2.1.9000) 于 2018 年 10 月 1 日创建

    【讨论】:

    • 非常感谢。@Tung
    • @Max:没问题。通过将date 转换为正确的格式,我已经使答案变得更好了
    【解决方案2】:

    我们也可以使用data.table中的dcast

    library(data.table)
    setDT(df)[, ID_Sum := sum(value), by = .(ID, date)]
    dcast(df, date + firms + value ~ paste0("Sum_", ID), value.var = 'ID_Sum', fill = 0)
    

    【讨论】:

      猜你喜欢
      • 2016-06-21
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多