【问题标题】:Summarizing grouped and ungrouped data in a single tibble with tidyverse in R在 R 中使用 tidyverse 在单个 tibble 中汇总分组和未分组的数据
【发布时间】:2021-09-02 19:07:31
【问题描述】:

为了总结数据中的每个变量,我通常会创建两个表:一个按实验条件分组,另一个显示所有实验组的汇总统计数据。

但是,我想在一个表格中同时显示分组和汇总的描述。

我已经完成了这个,如下面的演示代码所示——但是,我想找到一个更优雅的解决方案,最好使用单链。

有谁知道我如何做到这一点?

感谢您的帮助。


# Data
df <- tibble(Condition = rep(c("Group1",
                           "Group2",
                           "Group3",
                           "Group4"),20),
             comp_fail = rbinom(n = 80,
                                size = 1,
                                prob = .1)
)

# Converting Condition to factor to mimic my actual df
df$Condition <- as.factor(df$Condition)

# Descriptives grouped by condition
tbl_comp_fail_condition <- 
 df %>% 
  group_by(Condition) %>% 
  summarize("# with Zero Fails" = sum(comp_fail == 0, na.rm = T),
            "# with One Fail" = sum(comp_fail == 1, na.rm = T))

# Descriptives aggregated
tbl_comp_fail_aggregate <- 
 df %>% 
  summarize("# with Zero Fails" = sum(comp_fail == 0, na.rm = T),
            "# with One Fail" = sum(comp_fail == 1, na.rm = T))

# Joining grouped and aggregated
tbl_comp_fail_combined <- full_join(
 tbl_comp_fail_aggregate,
 tbl_comp_fail_condition) %>% 
  select(Condition, everything())

# Converting Condition to character to replace NA with All
tbl_comp_fail_combined$Condition <- as.character(tbl_comp_fail_combined$Condition)

# Replace NA with All
tbl_comp_fail_combined %>% replace_na(list(Condition = "All")) ``

【问题讨论】:

    标签: r grouping tidyverse summarize


    【解决方案1】:

    这是一种使用dplyr::bind_rows的方法:

    df %>%
      group_by(Condition) %>%
      summarize("# with Zero Fails" = sum(comp_fail == 0, na.rm = T),
                "# with One Fail" = sum(comp_fail == 1, na.rm = T)) %>%
      bind_rows(list(summarise(., across(-Condition, sum))))
    
    # A tibble: 5 x 3
      Condition `# with Zero Fails` `# with One Fail`
      <fct>                   <int>             <int>
    1 Group1                     18                 2
    2 Group2                     18                 2
    3 Group3                     17                 3
    4 Group4                     18                 2
    5 NA                         71                 9
    

    或者先获取摘要:

    df %>%
      group_by(Condition) %>%
      summarize("# with Zero Fails" = sum(comp_fail == 0, na.rm = T),
                "# with One Fail" = sum(comp_fail == 1, na.rm = T)) %>%
      bind_rows(list(summarise(., across(-Condition, sum))), .) %>%
      relocate(Condition)
    
    # A tibble: 5 x 3
      Condition `# with Zero Fails` `# with One Fail`
      <fct>                   <int>             <int>
    1 NA                         71                 9
    2 Group1                     18                 2
    3 Group2                     18                 2
    4 Group3                     17                 3
    5 Group4                     18                 2
    

    【讨论】:

      【解决方案2】:

      使用adorn_totals 会更容易

      library(dplyr)
      library(janitor)
      df %>% 
          group_by(Condition) %>% 
          summarise("# with Zero Fails" = sum(!comp_fail, na.rm = TRUE), 
                  "# with One Fail" = sum(comp_fail, na.rm = TRUE)) %>% 
          adorn_totals(name = "All")
      

      -输出

         Condition # with Zero Fails # with One Fail
          Group1                17               3
          Group2                14               6
          Group3                18               2
          Group4                14               6
             All                63              17
      

      OP 的输出

      out
      # A tibble: 5 x 3
        Condition `# with Zero Fails` `# with One Fail`
        <chr>                   <int>             <int>
      1 All                        63                17
      2 Group1                     17                 3
      3 Group2                     14                 6
      4 Group3                     18                 2
      5 Group4                     14                 6
      

      或者另一个选项是countpivot_wider(如果有超过 2 个组,这会更通用)

      library(tidyr)
      df %>%
           count(Condition, comp_fail) %>% 
           pivot_wider(names_from = comp_fail, values_from = n) %>% 
           adorn_totals(name = 'All')
      

      或者这可以在base Rtable/add_margins 中完成

      addmargins(table(df), 1)
               comp_fail
      Condition  0  1
         Group1 17  3
         Group2 14  6
         Group3 18  2
         Group4 14  6
         Sum    63 17
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-02
        • 1970-01-01
        • 1970-01-01
        • 2014-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多