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

发布于 2025-01-21 09:29:33 字数 816 浏览 0 评论 0原文

#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?
  

我想要这样的东西:

”在此处输入图像描述”

#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?
  

I want something like this:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三人与歌 2025-01-28 09:29:33

使用GT软件包,您可以将表送到tab_options(column_labels.hidden = true)以删除列标记。不幸的是,这将删除两个级别:列标题和包含您要保留的cyl信息的跨度标签。

请注意,dataSummary_balance()产生一个高度自定义的表,该表旨在用作现成的输出。在这种情况下,只需使用dataSummary()而不是尝试自定义dataSummary_balance()(Square peg,round peg,round hole,,, 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")

Using the gt package, you can pipe your table to tab_options(column_labels.hidden = TRUE) to remove column labels. Unfortunately, this will remove both levels: the column headers, and the spanning labels that include the cyl info you want to keep.

Note that datasummary_balance() produces a highly customized table which is intended to be used as a ready-made output. In cases like these, it might be easier to just build the custom table you want using datasummary() instead of trying to customize datasummary_balance() (square peg, round hole, etc). For example:

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

晒暮凉 2025-01-28 09:29:33

丑陋的解决方案,但人们总是可以添加cols_label(col_name =“”),作为一种使每个列名称无关的方式。

Ugly solution but one could always add cols_label(col_name = "") as a way of making every column name nothing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文