【问题标题】:Row sum as am extra column in the table in R行总和作为 R 表中的额外列
【发布时间】:2021-02-15 10:52:18
【问题描述】:

我正在尝试制作一个描述性表格。我首先计算每组和每年的观察次数。然后,我想添加另一列,其中包含每年的观察总和。

如何在不使用合并功能的情况下做到这一点?

year <- rep(2014:2015, length.out = 10000)
group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
value <- sample(10000, replace = T)

dta <- data.frame(year = year, group = group, value = value)

library(dplyr)
library(tidyr)

dta1 <- dta %>%
  group_by(year, group) %>%
  summarize(nobs = n()) %>%
  pivot_wider(names_from= group, values_from = nobs)

dta2 <- dta %>%
  group_by(year)%>%
  summarize(total_nobs_per_year = n())

table <- merge(dta1, dta2, by="year")

table

我想要的表格如下所示:

【问题讨论】:

    标签: r tidyr


    【解决方案1】:

    您可以从第二列开始使用基本 R 函数rowSums,如下所示:

    dta1$total_nobs_per_year<-rowSums(dta1[2:ncol(dta1)])
    dta1
    # A tibble: 2 x 9
    # Groups:   year [2]
       year   `0`   `1`   `2`   `3`   `4`   `5`   `6` total_nobs_per_year
      <int> <int> <int> <int> <int> <int> <int> <int>               <dbl>
    1  2014   738   711   712   709   656   750   724                5000
    2  2015   723   711   767   731   659   745   664                5000
    

    【讨论】:

      【解决方案2】:

      根据定义,您的total_nobs_per_year 将是dta1 中没有第一列的行的总和。你可以使用

      dta1 %>% 
        ungroup() %>% 
        mutate(total_nobs_per_year = rowSums(dta1[-1]))
      

      哪个产生

      # A tibble: 2 x 9
         year   `0`   `1`   `2`   `3`   `4`   `5`   `6` total_nobs_per_year
        <int> <int> <int> <int> <int> <int> <int> <int>               <dbl>
      1  2014   683   699   722   731   701   712   752                5000
      2  2015   704   689   734   706   726   709   732                5000
      

      【讨论】:

        【解决方案3】:

        由于您的数据框一开始就采用整齐(长)格式,因此您还可以在透视之前计算总和,从而无需两个表。只需使用mutate 而不是summarise 即可保留所有行:

        library(dplyr)
        library(tidyr)
        
        year <- rep(2014:2015, length.out = 10000)
        group <- sample(c(0,1,2,3,4,5,6), replace=TRUE, size=10000)
        value <- sample(10000, replace = T)
        dta <- data.frame(year = year, group = group, value = value)
        
        
        dta %>%
          group_by(year, group) %>%
          summarise(nobs = n(), .groups = "drop_last") %>%
          mutate(total_nobs_per_year = sum(nobs)) %>% 
          pivot_wider(names_from = group, values_from = nobs)
        #> # A tibble: 2 x 9
        #> # Groups:   year [2]
        #>    year total_nobs_per_year   `0`   `1`   `2`   `3`   `4`   `5`   `6`
        #>   <int>               <int> <int> <int> <int> <int> <int> <int> <int>
        #> 1  2014                5000   751   745   701   690   716   683   714
        #> 2  2015                5000   741   737   706   632   694   746   744
        

        这是可行的,因为mutate() 中的计算也是按组完成的(如summarise)。如果您想在不折叠组的情况下添加摘要列,这将非常有用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多