ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.04k stars 115 forks source link

Bug Report: requested continuous2 statistics not applied when N is low #1362

Closed PaulC91 closed 2 years ago

PaulC91 commented 2 years ago

Hey.

As you can see from the reprex below, the requested statistics are not applied when the sample size is reduced to 10. Perhaps this is intended behaviour but I can't find anything in the docs about it.

Thanks!

library(gtsummary)

trial %>%
  dplyr::slice_sample(n = 100) %>% 
  dplyr::select(age, marker) %>%
  tbl_summary(
    type = all_continuous() ~ "continuous2",
    statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}"),
    missing = "no"
  ) %>% 
  as_kable()
Characteristic N = 100
Age
Median (IQR) 48 (39, 57)
Range 6, 83
Marker Level (ng/mL)
Median (IQR) 0.61 (0.18, 1.42)
Range 0.01, 2.77
trial %>%
  dplyr::slice_sample(n = 10) %>% 
  dplyr::select(age, marker) %>%
  tbl_summary(
    type = all_continuous() ~ "continuous2",
    statistic = all_continuous() ~ c("{median} ({p25}, {p75})", "{min}, {max}"),
    missing = "no"
  ) %>% 
  as_kable()
Characteristic N = 10
Age
9 1 (11%)
34 1 (11%)
47 1 (11%)
50 1 (11%)
54 1 (11%)
57 1 (11%)
65 1 (11%)
67 1 (11%)
78 1 (11%)
Marker Level (ng/mL)
0.161 1 (11%)
0.169 1 (11%)
0.175 1 (11%)
0.238 1 (11%)
0.389 1 (11%)
1.107 1 (11%)
1.321 1 (11%)
1.328 1 (11%)
1.68 1 (11%)

Created on 2022-10-11 with reprex v2.0.2

ddsjoberg commented 2 years ago

You need to use the type argument to tell tbl_summary() that these variables are indeed continuous2 (they default with so few unique levels is categorical). Because their default is categorical, you can't use all_continuous() to cast them as continuous2. https://www.danieldsjoberg.com/gtsummary/reference/tbl_summary.html#type-argument-1

PaulC91 commented 2 years ago

Got it! thanks for the quick response.