【问题标题】:Print summary statistics in sub-groups of flextable object在 flextable 对象的子组中打印汇总统计信息
【发布时间】:2021-04-19 07:47:28
【问题描述】:

上下文:我正在尝试创建一个包含按组汇总统计信息的 docx 表。

问题:如何在每个组的顶部或底部添加汇总统计信息(例如sum)并获得“总计”行作为最后一行?

到目前为止,我使用flextable::as_grouped_data() 得到了很好的结果,如下所示:https://davidgohel.github.io/flextable/reference/as_grouped_data.html#see-also

示例:

library(dplyr) # feel free to use data.table if you prefer, I am just more used to dplyr
data_co2_2 <- CO2 %>% 
  group_by(Type, Treatment, conc) %>% 
  summarise(uptake = mean(uptake)) %>% 
  pivot_wider(names_from = Type, values_from = uptake)

data_co2_2 <- as_grouped_data(x = data_co2_2, groups = c("Treatment"))

输出:

data_co2
#>     Treatment conc   Quebec Mississippi
#> 1  nonchilled   NA       NA          NA
#> 3        <NA>   95 15.26667    11.30000
#> 4        <NA>  175 30.03333    20.20000
#> 5        <NA>  250 37.40000    27.53333
#> 6        <NA>  350 40.36667    29.90000
#> 7        <NA>  500 39.60000    30.60000
#> 8        <NA>  675 41.50000    30.53333
#> 9        <NA> 1000 43.16667    31.60000
#> 2     chilled   NA       NA          NA
#> 10       <NA>   95 12.86667     9.60000
#> 11       <NA>  175 24.13333    14.76667
#> 12       <NA>  250 34.46667    16.10000
#> 13       <NA>  350 35.80000    16.60000
#> 14       <NA>  500 36.66667    16.63333
#> 15       <NA>  675 37.50000    18.26667
#> 16       <NA> 1000 40.83333    18.73333

预期输出: 而不是“组”行中的NA,我想显示一个汇总统计信息(如子组的总和)。锦上添花:在表格底部显示“总分”。

【问题讨论】:

    标签: r flextable


    【解决方案1】:

    我想在“组”行中显示汇总统计信息(如子组的总和),而不是 NA。

    如果使用as_grouped_data() %&gt;% as_flextable(),这是不可能的。显示的值是组的名称。

    以下是一个命题:

    library(flextable)
    library(dplyr)
    library(tidyr)
    
    CO2 <- CO2 %>% 
      mutate(conc = as.character(conc))
    
    agg1 <- CO2 %>% 
      group_by(Type, Treatment, conc) %>% 
      summarise(uptake = mean(uptake), .groups = "drop") 
    
    agg2 <- CO2 %>% 
      group_by(Type, Treatment) %>% 
      summarise(uptake = mean(uptake), .groups = "drop") %>% 
      mutate(conc="Overall")
    
    agg3 <- CO2 %>% 
      group_by(Type) %>% 
      summarise(uptake = mean(uptake), .groups = "drop")  %>% 
      mutate(conc="Overall", Treatment = "Overall")
    
    
    all_data <- bind_rows(agg1, agg2, agg3) %>% 
      arrange(Type, Treatment, conc) %>% 
      pivot_wider(names_from = Type, values_from = uptake)
    
    as_grouped_data(x = all_data, groups = c("Treatment")) %>% 
      as_flextable() %>% 
      compose(i = ~ is.na(conc) & is.na(Treatment), 
              j = "conc", value = as_paragraph("avg for all conc")) %>% 
      compose(i = ~ is.na(conc) & is.na(Treatment), 
              j = "conc", value = as_paragraph("avg for all conc")) %>% 
      bold(bold = TRUE, i = ~!is.na(Treatment)) %>% 
      color(i= ~ conc %in% "Overall", color = "red") %>% 
      colformat_double(j = c("Quebec", "Mississippi"), digits = 1) 
    
    

    【讨论】:

    • 再次非常感谢,您的解决方案非常接近所需的输出。只是限制,为了在底部显示“整体”,可能需要“手动”设置因子级别以进行自定义排列。
    • 您对因素的看法是正确的。如果有任何问题,请在此处或 github issue 上敲响警钟。
    猜你喜欢
    • 2019-03-19
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多