Search code examples
variablesoutputgtsummary

gtsummary - summary table in wide format for multiple variables


Is there a way to produce a summary table for dataframe with multiple continuous variables (wide format) within gtsummary package? So I want to get a table with summary statistics in raws (like in multi-line display), for each variable in the same wide format as in the dataframe.

I've tried to use tbl_summary function, but all variables in output are arranged in long format.

The final table I want to get looks like in the picture. Sample three variables with summary statistics


Solution

  • here's an example how you could make this work

    library(gtsummary)
    packageVersion("gtsummary")
    #> [1] '1.7.2'
    
    # number of decimal places to round statistics
    decimals <- list(age = c(1, 2, 1), marker = c(3, 4, 3))
    
    # vector of column names to summarize
    c("age", "marker") |> 
      lapply(
        \(x) {
          trial |> 
            # rename and select variable
            select(var = all_of(x)) |> 
            tbl_summary(
              type = ~"continuous2",
              statistic = ~c("{mean} ({sd})", "{median}"),
              digits = ~decimals[[x]],
              label = ~"generic_label",
              missing = "no"
            ) |> 
            # update header to indcate the variable
            modify_header(stat_0 = glue::glue("**{x}**")) |> 
            # remove the header row
            remove_row_type(type = "header") |> 
            # remove the default indentation
            modify_table_styling(columns = label, text_format = "indent", undo_text_format = TRUE)
        }
      ) |> 
      tbl_merge(tab_spanner = FALSE) |> 
      as_kable() # convert to kable to display on stackoverflow
    
    Characteristic age marker
    Mean (SD) 47.2 (14.31) 0.916 (0.8593)
    Median 47.0 0.639

    Created on 2023-12-27 with reprex v2.0.2