#Preparing the data and loading packages
library(modelsummary);library(tidyverse);library(gt)
as_tibble(mtcars)
df <- mtcars %>% mutate(cyl_ = factor(cyl)) %>%
dplyr::select(cyl_, mpg, vs, am, hp, wt)
#Gets table of descriptive statistics about different subsets of the data
print(t1 <- datasummary_balance(~cyl_,
data = df,
output = "gt"))
#This hides the "Std. Dev." columns
t1 %>% cols_hide(c(3,5,7))
#Now I want to hide the "Mean" column labels, but I want to keep the "cyl_" value column labels. Any ideas how?
我想要这样的东西:
使用
gt
包,您可以将表通过管道连接到 tab_options(column_labels.hidden = TRUE)
以删除列标签。不幸的是,这将删除两个级别:列标题和包含您想要保留的cyl
信息的跨度标签。
请注意,
datasummary_balance()
会生成一个高度定制的表格,旨在用作现成的输出。在这种情况下,使用 datasummary()
构建您想要的自定义表格可能会更容易,而不是尝试自定义 datasummary_balance()
(方钉、圆孔等)。例如:
library(modelsummary)
library(tidyverse)
df <- mtcars %>%
select(cyl, mpg, vs, am, hp, wt) %>%
mutate(cyl = factor(sprintf("%s (N = %s)", cyl, n()))) %>%
as.data.frame() # The `All()` function does not accept tibbles
datasummary(
All(df) ~ Mean * cyl,
data = df,
output = "gt")
丑陋的解决方案,但人们总是可以添加 cols_label(col_name = "") 作为一种使每个列名称都没有的方法。