arshad
arshad

Reputation: 437

gtsummary: How to add_n() for paired samples in gt_summary tbl_summary?

I want to use add_n() for paired samples. As this is a paired sample, the participants in the group1 and group2 are same. add_n() shows the total number of observation in 2 groups (i.e. in my case, actual number of participants x 2). How to show the participant's count using add_n() ? Any help, highly appreciated.

sample data:

ss <- structure(list(Day = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3), value = c(31.92, 66.32, 53.25, 37.34, 34.69, 33.32, 
33.32, 25.94, 34.69, 25.94, 37.34, 22.65, 38.62, 22.65, 37.34, 
73.15, 82.87, 33.32, 24.33, 31.92, 64.04, 46.54, 31.92, 44.74, 
37.34, 145.12, 37.34, 39.89, 42.35, 42.35, 25.94, 22.65, 22.65, 
34.69, 66.99, 60.87, 27.5, 25.94), id = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -38L))

ss %>% 
tbl_summary(by = Day, include = -id,  type = all_continuous() ~ "continuous2", label = list(value ~ p), 
statistic = list(all_continuous() ~ "{mean} ({sd})"))  %>%  
add_difference(test = list(value ~ "paired.t.test"), group = id) %>% 
add_n() %>% modify_header(all_stat_cols() ~ "**{level}**") 

Upvotes: 0

Views: 353

Answers (1)

Arthur K.
Arthur K.

Reputation: 142

To show the participant's count instead of the total number of observations in both groups, you can divide the add_n() result by 2. Here's the modified code:

ss %>% 
  tbl_summary(by = Day, include = -id, type = all_continuous() ~ "continuous2", 
              label = list(value ~ p), 
              statistic = list(all_continuous() ~ "{mean} ({sd})")) %>%  
  add_difference(test = list(value ~ "paired.t.test"), group = id) %>% 
  add_n() %>% 
  modify_header(all_stat_cols() ~ "**{level}**") %>% 
  modify_footnote(everything() ~ str_glue("**N (participants): {add_n()/2}**"))

This will divide the add_n() result by 2 and display it as "N (participants)" in the table footnote.

Upvotes: 0

Related Questions