Search code examples
rgtsummary

Omit variable label in gtsummary tbl_summary


Using gtsummary version 1.7.0, I am making a table with some statistics of a continuous variable:

library(gtsummary)
library(dplyr)
dat <- data.frame(x = 1:100, y = c(rep("A", 50), rep("B", 50)))
dat %>%
  tbl_summary(
    by = y,
    type = x ~ "continuous2",
    statistic = x ~ c("{mean}", "{min}", "{max}"),
    label = x ~ "this row should not exist"
  ) %>%
  modify_caption(caption = "Some basic statistics for variable x")

As I only have one variable, and the description of the variable will be in the table caption, I want to omit the variable label. Changing "this row should not exist" to NULL (shows "x" instead) or to "" (shows an empty line instead) does not quite get me there, because I want to get rid of that whole line. Is there a way to accomplish this?


Solution

  • You can use the modify_table_body() function to header row. Example below!

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.7.1'
    
    dat <- data.frame(x = 1:100, y = c(rep("A", 50), rep("B", 50)))
    dat |>
      tbl_summary(
        by = y,
        type = x ~ "continuous2",
        statistic = x ~ c("{mean}", "{min}", "{max}")
      ) |>
      # remove header row
      modify_table_body(
        ~ .x |> 
          dplyr::filter(!(variable %in% "x" & row_type %in% "label"))
      ) |> 
      as_kable() # convert to kable to display on stackoverflow
    
    Characteristic A, N = 50 B, N = 50
    Mean 26 76
    Minimum 1 51
    Maximum 50 100

    Created on 2023-05-09 with reprex v2.0.2