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

标签 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?
  

我想要这样的东西:

enter image description here

最佳答案

使用 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")

enter image description here

关于r - 如何删除 gt 表中的一行列标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71850738/

相关文章:

r - 数据框中的随机列样本

r - hist() 错误 : 'breaks' do not span range of 'data' and barplot(, log = "y") 错误:'高度 + 偏移量 <= 0

r - gt包如何格式化列摘要行: how to format summary values with numbers AND percent based on columns

r - 通过 modelplot() 的正/负值更改绘图颜色

r - Zoo merge() 和合并的列名

r - 缩小使用 R 中的传单创建的世界地图时的多个世界副本

r - 如何在R中使用map迭代对象以创建一系列gt表

r - 如何使用 gt() 为每个其他类别着色?

r - 生成具有 `partykit:mob()` 对象的并排节点模型的表