1

为一个非常基本的问题道歉......我喜欢使用expss包来创建表格,但是在处理一些输出显示时遇到了麻烦。具体来说,我的数据框包含一个分组变量以及一些将要汇总的变量。我想创建输出,依次显示子组的每个值(分组变量的每个值)加上整个样本的总数的某些汇总统计信息。类似于下面的代码,但将output1output2对象一起附加到一个表中,该表维护expss的 RStudio 查看器输出的格式。

library(expss)

set.seed(12345)

df <- data.frame(group = rep(1:5, each = 4),
                 varA = sample(1:4, 20, replace = TRUE),
                 varB = sample(6:9, 20, replace = TRUE))

output1 <- df[df$group == 1, ] %>%
  tab_cells(varA, varB) %>%
  tab_cols(total(label = "")) %>% 
  tab_stat_fun("Valid N" = w_n, "Mean" = w_mean, "SD" = w_sd,
               "Median" = w_median, method = list) %>% 
  tab_pivot() %>% 
  set_caption("Group 1")

output2 <- df %>%
  tab_cells(varA, varB) %>%
  tab_cols(total(label = "")) %>% 
  tab_stat_fun("Valid N" = w_n, "Mean" = w_mean, "SD" = w_sd,
               "Median" = w_median, method = list) %>% 
  tab_pivot() %>% 
  set_caption("All Groups")

expss_output_viewer()

output1
output2

我知道我可以添加tab_rows(group)到显示所有组的管道中;但是,我只对依次显示每个组(加上总数)而不是所有组感兴趣以进行输出。

4

1 回答 1

1

子组有特殊功能tab_subgroup

library(expss)

set.seed(12345)

df <- data.frame(group = rep(1:5, each = 4),
                 varA = sample(1:4, 20, replace = TRUE),
                 varB = sample(6:9, 20, replace = TRUE))

output <- df %>%
    tab_cells(varA, varB) %>%
    tab_cols(total(label = "")) %>% 
    tab_subgroup(group == 1) %>% 
    tab_row_label("Group 1") %>% 
    tab_stat_fun("Valid N" = w_n, "Mean" = w_mean, "SD" = w_sd,
                 "Median" = w_median, method = list) %>% 
    tab_row_label("All Groups") %>% 
    tab_subgroup() %>% 
    tab_stat_fun("Valid N" = w_n, "Mean" = w_mean, "SD" = w_sd,
                 "Median" = w_median, method = list) %>% 
    tab_pivot()

expss_output_viewer()

output

或者,您可以使用tab_rowsand net

library(expss)

set.seed(12345)

df <- data.frame(group = rep(1:5, each = 4),
                 varA = sample(1:4, 20, replace = TRUE),
                 varB = sample(6:9, 20, replace = TRUE))
output <- df %>%
    tab_cells(varA, varB) %>%
    tab_cols(total(label = "")) %>% 
    tab_rows(net(group, "Group 1" = 1,  "All Groups" = 1:5, position = "above")) %>%
    tab_stat_fun("Valid N" = w_n, "Mean" = w_mean, "SD" = w_sd,
                 "Median" = w_median, method = list) %>% 
    tab_pivot()

expss_output_viewer()

output
于 2019-12-04T09:53:21.647 回答