Search code examples
gtsummary

Omit categories from table


The following code

library(tidyverse) 
library(gtsummary)

df <- tibble(category = c("a", "a", "a", 
                    "b", "b", "b", "b"))

output_table <- df %>%
  tbl_summary()

output_table 

produces this table.

Is it possible to remove the "a" category from this table without changing the associated frequencies? So in this case, the final table should look like this (but without the extra whitespace).


Solution

  • Using the tbl_summary(value=) argument, you can select a single level of a categorical variable to display in the table.

    library(gtsummary)
    
    trial %>%
      select(grade) %>%
      # show only one level for grade
      tbl_summary(value = grade ~ "I", 
                  label = grade ~ "Grade I") 
    

    enter image description here

    You can also just delete a single row from the output. But you'll need to install the development version of the package to use the new function modify_table_body().

    remotes::install_github("ddsjoberg/gtsummary")
    trial %>%
      select(grade) %>%
      tbl_summary() %>%
      # remove grade I row
      modify_table_body(filter, !(variable == "grade" & label == "I"))
    

    enter image description here