How do you calculate a success rate for a column in tbl_summary using a different column value?

63 Views Asked by At

My question can be conceptualized using the same sample data used for the tbl_summary trial dataset from the tutorial here: https://www.danieldsjoberg.com/gtsummary/articles/tbl_summary.html.

How would I calculate the mortality rate based on grade of tumor instead of calculating the incidence of grade I-III tumors? So far as I can find, there is not a way to calculate a custom statistic based on a third column, which is essentially what I am trying to do.

--- Edit ---

My input data has 3 columns - stent_type, success, stent_30. stent_type is either a category 0 or 1, success is either a 0 or 1, and stent_30 is either a 0 or 1 (basically was the stent placed before or after 30 days). I would like to create a 2x3 table with the percent/number of successfully placed stents for category 0 or 1 stents, along with a p-value comparing the two columns for stent_30. Something like this:

Stent < 30 Stent > 30 p-value
stent 0 75%(10) 70% (9) 0.1
stent 1 50% (6) 40% (2) 0.05

I'm basically having trouble because I'm not sure how to summarize a category based on 2 different categories (if that makes sense?)

1

There are 1 best solutions below

3
Jay Bee On

You can do this with general tidyverse operations (group_by and summarise).

# Load the required libraries.

library(gtsummary)
library(tidyverse)

# Get the mortality counts.

mortality_counts <- trial %>%
  group_by(grade, death) %>%
  summarise(count = n()) %>%
  ungroup()

# Get the mortality proportions.

mortality_proportions <- mortality_counts %>%
  group_by(grade) %>%
  mutate(total = sum(count),
         proportion = count / total)

With the output of mortality_proportions as:

grade death count total proportion
I 0 35 68 0.5147059
I 1 33 68 0.4852941
II 0 32 68 0.4705882
II 1 36 68 0.5294118
III 0 21 64 0.3281250
III 1 43 64 0.6718750

If you wanted to make that clearer just to show the grade and proportions, you could do:

mortality_proportions %>%
  filter(death == 1) %>%
  select(grade, proportion) %>%
  mutate(proportion = round(proportion, 2))

For output:

grade proportion
I 0.49
II 0.53
III 0.67