在单个表达式中添加和堆叠子组 table

Add and stack subgroups in a single expss table

这次是一个特殊请求,因为我知道如何获得我想要的 table 输出,但想知道 expss 是否存在一个不那么冗长的解决方案。首先,这个主题可以被认为是这个讨论的延伸 --> , and is also related to this other one -->

我的 table 构造如下:首先显示总数据帧行的结果,然后按子组拆分。截至今天,以下是我的处理方式(以 infert 数据集为例):

1) Table 模板

### Banner set up
my_banner = infert %>%
  tab_cols(total())
my_custom_table = . %>%  
  tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
  tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
  tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  # Parity x Education
  tab_cols(education) %>%
  tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
  tab_last_add_sig_labels() %>%
  tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct(label="T.1", compare_type="subtable")

2) 创建 3 个不同的 table(总共 1 个,每个子组 1 个),合并为一个:

tab1 <- my_banner %>%
  tab_cells(parity) %>%
  my_custom_table() %>%
  tab_pivot(stat_position="inside_columns")
tab2 <- infert %>%
  apply_labels(education="education (CASE 0)") %>%
  tab_cells(parity) %>%
  tab_cols(total(label = "CASE 0")) %>%
  tab_subgroup(case==0) %>%
  my_custom_table() %>%
  tab_pivot(stat_position="inside_columns")
tab3 <- infert %>%
  apply_labels(education="education (CASE 1)") %>%
  tab_cells(parity) %>%
  tab_cols(total(label = "CASE 1")) %>%
  tab_subgroup(case==1) %>%
  my_custom_table() %>%
  tab_pivot(stat_position="inside_columns")

final_tab <- tab1 %merge% tab2 %merge% tab3

全部代码仅供1人使用table,您理解我的担心。有什么好的做法可以避免这种冗长(但有效)的序列?我的第一个猜测是:

my_banner %>%
  tab_cells(parity) %>%
  my_custom_table() %>%
  tab_subgroup(case==0) %>%
  my_custom_table() %>%
  tab_subgroup(case==1) %>%
  my_custom_table() %>%
  tab_pivot(stat_position="inside_columns")

A table 已计算出来,但输出与 objective 相去甚远,可能有修复,但我不知道去哪里寻找。任何帮助将不胜感激,谢谢! (注意:如果一个简单的解决方案涉及删除#TOTAL 列,那对我来说也很好)

关键思想是在 tab_cols 中使用 %nest% 而不是 tab_subgroup:

library(expss)
data(infert)
my_banner = infert %>%
    apply_labels(
        education = "education",
        case = c(
            "CASE 0" = 0,
            "CASE 1" = 1
        )
    ) %>% 
    tab_cols(total(), education, case %nest% list(total(label = ""), education))

my_custom_table = . %>%  
    tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
    tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
    tab_last_add_sig_labels() %>%
    tab_stat_cpct(label="%Col.",
                  total_row_position="above", 
                  total_statistic=c("u_cases", "u_cpct"), 
                  total_label=c("TO_DELETE_TOTAL", "TOTAL")) %>%
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct(label="T.1", compare_type="subtable") %>% 
    tab_pivot(stat_position="inside_columns") %>% 
    # drop auxilary rows and columns
    where(!grepl("TO_DELETE", row_labels)) %>% 
    except(fixed("Total|T.1"), fixed("CASE 0|T.1"), fixed("CASE 1|T.1"))

my_banner %>% 
    tab_cells(parity) %>% 
    my_custom_table()