如何删除gt表中的一行列标签?

tci*_*tci 3 r gt modelsummary

#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?
  
Run Code Online (Sandbox Code Playgroud)

我想要这样的东西:

在此输入图像描述

Vin*_*ent 5

使用该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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述