Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tbl_stack of 2 tbl_summary objects with different N #577

Closed
JesseRop opened this issue Jul 18, 2020 · 1 comment
Closed

tbl_stack of 2 tbl_summary objects with different N #577

JesseRop opened this issue Jul 18, 2020 · 1 comment

Comments

@JesseRop
Copy link

JesseRop commented Jul 18, 2020

This is an issue related to #234 (#234) but for tbl_stack.
Stacking 2 tbl_summary objects with different numbers result only the Ns of the first object being displayed in the header. Kindly inform if it is possible to have the 2 stacked objects with separate Ns for instance in the "group_header " rows separating the stacked tables?

library(survival)
#> Warning: package 'survival' was built under R version 3.6.3
library(gtsummary)
#> Warning: package 'gtsummary' was built under R version 3.6.3

# make dataset that is arbitrarily different size
trial2 <- trial %>%
  dplyr::filter(age > 30)

t1 <- trial %>%
  select(response, trt, grade, age) %>%
  tbl_summary(by = trt) %>% 
  add_p(pvalue_fun = function(x) style_pvalue(x, digits = 2)) %>% 
  add_stat_label()

t1

Characteristic

Drug A, N = 98

Drug B, N = 102

p-value1

Tumor Response, n (%)

28 (29%)

33 (34%)

0.64

Unknown

3

4

Grade, n (%)

0.87

I

35 (36%)

33 (32%)

II

32 (33%)

36 (35%)

III

31 (32%)

33 (32%)

Age, yrs, median (IQR)

46 (37, 59)

48 (39, 56)

0.72

Unknown

7

4

1

Statistical tests performed: chi-square test of independence; Wilcoxon rank-sum test

# table with different sample size
t2 <- trial2 %>%
  select(response, trt, grade, age) %>%
  tbl_summary(by = trt) %>% 
  add_p(pvalue_fun = function(x) style_pvalue(x, digits = 2)) %>% 
  add_stat_label()

t2

Characteristic

Drug A, N = 83

Drug B, N = 88

p-value1

Tumor Response, n (%)

26 (32%)

28 (33%)

0.99

Unknown

2

4

Grade, n (%)

0.81

I

30 (36%)

28 (32%)

II

26 (31%)

31 (35%)

III

27 (33%)

29 (33%)

Age, yrs, median (IQR)

48 (38, 60)

49 (43, 57)

0.55

1

Statistical tests performed: chi-square test of independence; Wilcoxon rank-sum test

# stacked table
t <- tbl_stack(
  tbls = list(t1, t2),
  group_header = c("All", "Over 30")
)

t

Characteristic

Drug A, N = 98

Drug B, N = 102

p-value1

All

Tumor Response, n (%)

28 (29%)

33 (34%)

0.64

Unknown

3

4

Grade, n (%)

0.87

I

35 (36%)

33 (32%)

II

32 (33%)

36 (35%)

III

31 (32%)

33 (32%)

Age, yrs, median (IQR)

46 (37, 59)

48 (39, 56)

0.72

Unknown

7

4

Over 30

Tumor Response, n (%)

26 (32%)

28 (33%)

0.99

Unknown

2

4

Grade, n (%)

0.81

I

30 (36%)

28 (32%)

II

26 (31%)

31 (35%)

III

27 (33%)

29 (33%)

Age, yrs, median (IQR)

48 (38, 60)

49 (43, 57)

0.55

1

Statistical tests performed: chi-square test of independence; Wilcoxon rank-sum test

Created on 2020-07-18 by the reprex package (v0.3.0)

@ddsjoberg
Copy link
Owner

ddsjoberg commented Jul 18, 2020

HI @JesseRop !

I am generally in favor of these kinds of consistency checks. The issue we'd encounter implementing something like this is that tbl_stack() is a general function that accepts any type of {gtsummary} object (and this includes objects outside the package that follow the general gtsummary structure with class gtsummary). Because there are may different type of objects that can be stacked, adding checks can be tricky.

For this example, I would recommend removing the Ns from the header to a column with the add_n() function, like in the example below. Happy Coding! 🕺

t1 <- 
  trial %>% 
  select(age, grade, trt) %>% 
  tbl_summary(by = trt, missing = "no") %>%
  add_n() %>%
  modify_header(stat_by = "**{level}**")
t2 <- 
  trial %>%
  filter(age >= 30) %>%
  select(age, grade, trt) %>% 
  tbl_summary(by = trt, missing = "no") %>%
  add_n()

tbl_stack(list(t1, t2), group_header = c("All Patients", "Over 30"))

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants