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

Phil
  • 7,287
  • 3
  • 36
  • 66
tci
  • 69
  • 7

1 Answers1

5

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

Vincent
  • 15,809
  • 7
  • 37
  • 39
  • 1
    Nice. I have spent ages fiddling with this. I had managed to use css (with tab_style and locations) to hide the desired header but couldn't get rid of one of the borders so had slightly thicker lines in places above data row 1. I think might be possible through css but would be hiding not removing. – QHarr Apr 13 '22 at 03:24
  • Very cool, @Vincent. It works if cyl is numeric. However, in my real data, "cyl" is actually a character or factor variable. Like if cyl was ('four', 'six', 'eight'). How would your fix for getting "(N=11)" in the label work in that situation? – tci Apr 13 '22 at 13:22
  • Maybe insert a `group_by(cyl)` call just before mutate. That way the `n()` function will count the number of rows within each `cyl` stratum. – Vincent Apr 13 '22 at 19:51