#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()
(方钉、圆孔、 ETC)。例如:
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")
关于r - 如何删除 gt 表中的一行列标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71850738/