jrdusen
jrdusen

Reputation: 11

gtsummary add_stat_label location='column' for categorical and continuous variables

I'm trying to use add_stat_label in gtsummary to create a Statistic column. It seems that no matter what I try, I cannot get the continuous statistics to appear in the Statistic column, they continue to show under the Characteristic column.

Reproducible example:

iris_table = iris |>
  select(Species, Sepal.Length) |>
  tbl_summary(
    type = Sepal.Length ~ 'continuous2',
    statistic = list(
      all_continuous() ~ 
        c('{mean} ({sd})', '{median} ({p25}-{p75})', '{min}, {max}'),
      all_categorical() ~ '{n} ({p})')
  ) |>
  add_stat_label(label = 
                   list(all_continuous() ~ c('Mean (SD)', 'Median (Q1-Q3)', 'Min, max'),
                        all_categorical() ~ 'N (%)'), 
                 location =  'column')

iris_table

output iris table

In the example above, the continuous variable labels (mean, median, etc.) appear under Sepal.Length. I want them to the right, in the Statistic column just like how the N (%) labels appear for the species variable.

I also tried

add_stat_label(location = 'column')

but got the same result (without the nice labels)

I'm running R 4.4.2, gtsummary 2.0.4, dplyr 1.1.4

Upvotes: 1

Views: 45

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11774

Ahhh, you're using the function in a way that I hadn't planned for. The column placement was planned for categorical, dichotomous, and continuous summary types (not continuous2, since those stats already appear in the table).

To get what you're after, it'll be easiest to just update the data frame that is printed directly.

library(gtsummary)

tbl <- iris |>
  select(Species, Sepal.Length) |>
  tbl_summary(
    type = Sepal.Length ~ 'continuous2',
    statistic = list(
      all_continuous() ~ 
        c('{mean} ({sd})', '{median} ({p25}-{p75})', '{min}, {max}'),
      all_categorical() ~ '{n} ({p})')
  ) |>
  add_stat_label(
    label = 
      list(all_continuous() ~ c('Mean (SD)', 'Median (Q1-Q3)', 'Min, max'),
           all_categorical() ~ 'N (%)'), 
    location =  'column'
  ) |> 
  modify_table_body(
    ~ .x |> 
      dplyr::mutate(
        # populate the continuous stats in the stat_label column
        stat_label = 
          ifelse(
            var_type == "continuous2" & row_type == "level",
            label, 
            stat_label
          ),
        label = ifelse(var_type == "continuous2" & row_type == "level", NA, label)
      )
  )
tbl |> as_kable()
Characteristic Statistic N = 150
Species
setosa N (%) 50 (33)
versicolor N (%) 50 (33)
virginica N (%) 50 (33)
Sepal.Length
Mean (SD) 5.84 (0.83)
Median (Q1-Q3) 5.80 (5.10-6.40)
Min, max 4.30, 7.90

Created on 2024-12-11 with reprex v2.1.1

Upvotes: 1

Related Questions