cake2244
cake2244

Reputation: 29

Fixing table for gtsummary in R

I am trying to create a table using gtsummary. To give some insight, here is my current code so far:

# variables associated with Interpreter Use
table1 <- tbl_summary(pre_int, include = c('Age', 'Gender_Identity'), 
by = 'Interpreter Used') %>%
add_n() %>% 
modify_header(label = "**Variable**") %>% 
bold_labels() %>% add_p()

I am getting the following output as an example: enter image description here

I want to add a few things to the code:

  1. Fix the percentages such that they display to two significant digits
  2. Get the p-values generated for the subcategories rather than just the main categories (for example generating a p-value for gender identity female, male, etc.)
  3. Get the mean and SD for age rather than median and IQR.

Any help on how to format the code in order to add these changes? Would appreciate any help. I've tried to look at the documentation but I wasn't able to adapt this code accordingly.

To experiment, here's some sample data:

Age Gender Identity Interpreter Used
7 White non-Hispanic Yes
15 Other No
8 Black non-Hispanic No
10 White non-Hispanic No
8 White non-Hispanic Yes
15 White non-Hispanic Yes

Thank you!

Upvotes: 0

Views: 270

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11680

  1. Fix the percentages such that they display to two significant digits

Use the argument tbl_summary(digits = all_categorical() ~ c(0, 2)

  1. Get the p-values generated for the subcategories rather than just the main categories (for example generating a p-value for gender identity female, male, etc.)

P-values are added an a per-variable basis. To get a p-value for each level of a categorical variable, you must create dummy variables for each level, then add them to the table.

  1. Get the mean and SD for age rather than median and IQR.

Use the argument tbl_summary(statistic = age ~ "{mean} ({sd})")

Upvotes: 0

Related Questions