Reputation: 423
I need help on how to write R code using gtsummary package to create a summary table with several categorical variables as rows and the column side (the "by" variable) is a numeric variable in my case, age in years. So in essence I would like to summarize several patient categorical characteristics by their mean/median age.
As an example, in this package, with the data "trial", I would like for instance to have on the row axis of the table the categorical variables (marker, stage, grade) while the by variable is "age", so median age for each category of those variables.
Thank you for help. Nelly
Upvotes: 1
Views: 1450
Reputation: 507
Even though this is an old question, I though it was worth noting (for future readers) that gtsummary currently has a tbl_custom_summary()
function which does make this possible:
library(tidyverse)
library(gtsummary)
my_fn <- function(data, full_data, variable, by, type, ...) {
mean_age <- mean(data$age, na.rm = TRUE) %>% round(1) %>% format(nsmall = 1)
age_sd <- sd(data$age, na.rm = TRUE) %>% round(1) %>% format(nsmall = 1)
tibble(age = paste0(mean_age, " (", age_sd, ")"))
}
trial %>%
select(c(grade, trt, stage, age, response)) %>%
mutate(response = factor(response)) %>%
tbl_custom_summary(
by = response,
stat_fns = all_categorical() ~ my_fn,
statistic = all_categorical() ~ "{age}",
include = c(grade, trt, stage)
)
Upvotes: 0
Reputation: 11680
I am not 100% clear on what you're asking. I am guessing you want to summarize data by high age and low age (split at the median in the example below)?
First, you will want to create a categorical age variable.
library(gtsummary)
library(tidyverse)
df_age_example <-
trial %>%
mutate(
# create a categorical age variable split at the median
age2 = ifelse(
age >= median(.$age, na.rm = TRUE),
"Age Above or at Median",
"Age Below Median"
)
) %>%
# keep variables to be summarized
select(age2, marker, grade)
Then you'll want to pass that data frame to tbl_summary()
to summarize the data.
tbl_summary(data= df_age_example, by = age2)
That will yield the table below.
I hope this helps. Happy Coding!
Upvotes: 1