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

问题描述 投票:0回答:2
#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?
  

我想要这样的东西:

r gt modelsummary
2个回答
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")


0
投票

丑陋的解决方案,但人们总是可以添加 cols_label(col_name = "") 作为一种使每个列名称都没有的方法。

© www.soinside.com 2019 - 2024. All rights reserved.