r - 如何在 expss 表中仅显示选择子组 + 整个数据框的结果?
问题描述
为一个非常基本的问题道歉......我喜欢使用expss包来创建表格,但是在处理一些输出显示时遇到了麻烦。具体来说,我的数据框包含一个分组变量以及一些将要汇总的变量。我想创建输出,依次显示子组的每个值(分组变量的每个值)加上整个样本的总数的某些汇总统计信息。类似于下面的代码,但将output1
和output2
对象一起附加到一个表中,该表维护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
and 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
推荐阅读
- php - 为什么我收到此 mbstring 扩展丢失错误
- ios - 无法从 Android Studio 为模拟器构建应用程序
- keras - 如何获得 keras-saved-model 的权重
- r - 将 R 与 Google Colaboratory 结合使用
- spring - 无法在 Spring Boot 上加入两个表
- google-cloud-platform - 如何在 GCP 中使用“compute@developer.gserviceaccount.com”类型的服务帐户创建 kubernetes 集群?
- javascript - DataTable 导出带图像的表格数据(PDF、Excel 和 csv)
- c# - 编写 UnityWebRequest.Post 的问题
- android - Delphi TListView DynamicApperance 适用于 Windows,但不适用于 Android
- python - 将列分成两部分,同时在熊猫中保留分隔符