如何在 expss table 中仅显示 select 个子组 + 整个数据框的结果?
How to display results from only select subgroups + the whole data frame in an expss table?
抱歉,这是一个非常基本的问题...我喜欢使用 expss 包来创建 table,但在解决某些问题时遇到了问题输出显示。具体来说,我的数据框包含一个分组变量以及一些要汇总的变量。我想创建输出,显示子组 的每个值依次 (分组变量的每个值)加上整个样本的总数的某些汇总统计信息。类似于下面的代码,但是将 output1
和 output2
对象附加在一个 table 中,保持 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)
添加到将显示所有组的管道;但是,我只对依次显示每个组(加上总数)感兴趣,不是所有组,以供输出。
子群有特殊功能: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_rows
和 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
抱歉,这是一个非常基本的问题...我喜欢使用 expss 包来创建 table,但在解决某些问题时遇到了问题输出显示。具体来说,我的数据框包含一个分组变量以及一些要汇总的变量。我想创建输出,显示子组 的每个值依次 (分组变量的每个值)加上整个样本的总数的某些汇总统计信息。类似于下面的代码,但是将 output1
和 output2
对象附加在一个 table 中,保持 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)
添加到将显示所有组的管道;但是,我只对依次显示每个组(加上总数)感兴趣,不是所有组,以供输出。
子群有特殊功能: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_rows
和 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