Here are some fake data with some potential grouping vars (e.g., nationality) and different numeric vars.
demo_dat <- read.csv("data/data_SummaryTableExport_demo.csv")
demo_dat$ID <- as.factor(demo_dat$ID)
demo_dat$Nation <- as.factor(demo_dat$Nation)
glimpse(demo_dat)
## Rows: 20
## Columns: 7
## $ ID <fct> A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T
## $ Nation <fct> USA, USA, USA, USA, USA, USA, USA, USA, USA, USA, FRANCE, FRANC…
## $ Height <int> 50, 53, 86, 88, 57, 92, 85, 52, 54, 45, 69, 41, 88, 50, 45, 61,…
## $ Weight <int> 260, 192, 227, 116, 219, 106, 120, 192, 280, 260, 153, 167, 202…
## $ IQ <int> 87, 108, 100, 107, 112, 87, 102, 97, 84, 123, 129, 81, 108, 126…
## $ Income <int> 52506, 26067, 177605, 165289, 70161, 118671, 29011, 161817, 967…
## $ MMSE <int> 12, 10, 9, 6, 3, 7, 4, 13, 2, 8, 29, 17, 24, 26, 30, 20, 18, 22…
Summarize dataframe aggregated by ID and Nation. View table, export as APA
library(flextable)
demo_sum <- demo_dat %>%
group_by(Nation) %>%
summarize(across(where(is.numeric), list(mean = ~mean(.x, na.rm = TRUE), sd = ~sd(.x,
na.rm = TRUE), min = ~min(.x, na.rm = TRUE), max = ~max(.x, na.rm = TRUE)))) %>%
ungroup()
# Convert summary dataframe to flextable
demo_table <- flextable::flextable(demo_sum)
demo_table <- colformat_double(x = demo_table, digits = 2) #export to word from flextable
print(demo_table)
## a flextable object.
## col_keys: `Nation`, `Height_mean`, `Height_sd`, `Height_min`, `Height_max`, `Weight_mean`, `Weight_sd`, `Weight_min`, `Weight_max`, `IQ_mean`, `IQ_sd`, `IQ_min`, `IQ_max`, `Income_mean`, `Income_sd`, `Income_min`, `Income_max`, `MMSE_mean`, `MMSE_sd`, `MMSE_min`, `MMSE_max`
## header has 1 row(s)
## body has 2 row(s)
## original dataset sample:
## Nation Height_mean Height_sd Height_min Height_max Weight_mean Weight_sd
## 1 FRANCE 63.3 19.41963 41 91 196.3 64.84863
## 2 USA 66.2 18.87856 45 92 197.2 64.14532
## Weight_min Weight_max IQ_mean IQ_sd IQ_min IQ_max Income_mean Income_sd
## 1 103 299 104.0 18.72610 76 129 105967.8 49338.40
## 2 106 280 100.7 12.41907 84 123 109033.7 63114.71
## Income_min Income_max MMSE_mean MMSE_sd MMSE_min MMSE_max
## 1 29639 176995 22.3 4.738729 17 30
## 2 26067 192466 7.4 3.717825 2 13
demo_table %>%
save_as_docx(path = "figs/Demo_FlexTableOutput.docx")
library(stargazer)
# Fit sample regression models
model1 <- lm(mpg ~ wt, data = mtcars)
model2 <- lm(mpg ~ wt + hp, data = mtcars)
# Print a basic text table to the console
stargazer(model1, model2, type = "text")
##
## =================================================================
## Dependent variable:
## ---------------------------------------------
## mpg
## (1) (2)
## -----------------------------------------------------------------
## wt -5.344*** -3.878***
## (0.559) (0.633)
##
## hp -0.032***
## (0.009)
##
## Constant 37.285*** 37.227***
## (1.878) (1.599)
##
## -----------------------------------------------------------------
## Observations 32 32
## R2 0.753 0.827
## Adjusted R2 0.745 0.815
## Residual Std. Error 3.046 (df = 30) 2.593 (df = 29)
## F Statistic 91.375*** (df = 1; 30) 69.211*** (df = 2; 29)
## =================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
library(gtsummary)
# data in package
head(trial)
| trt | age | marker | stage | grade | response | death | ttdeath |
|---|---|---|---|---|---|---|---|
| Drug A | 23 | 0.16 | T1 | II | 0 | 0 | 24.00 |
| Drug B | 9 | 1.11 | T2 | I | 1 | 0 | 24.00 |
| Drug A | 31 | 0.28 | T1 | II | 0 | 0 | 24.00 |
| Drug A | NA | 2.07 | T3 | III | 1 | 1 | 17.64 |
| Drug A | 51 | 2.77 | T4 | III | 1 | 1 | 16.43 |
| Drug B | 39 | 0.61 | T4 | I | 0 | 1 | 15.64 |
summary(trial)
## trt age marker stage grade
## Length:200 Min. : 6.00 Min. :0.0030 T1:53 I :68
## Class :character 1st Qu.:38.00 1st Qu.:0.2150 T2:54 II :68
## Mode :character Median :47.00 Median :0.6385 T3:43 III:64
## Mean :47.24 Mean :0.9160 T4:50
## 3rd Qu.:57.00 3rd Qu.:1.3930
## Max. :83.00 Max. :3.8740
## NA's :11 NA's :10
## response death ttdeath
## Min. :0.0000 Min. :0.00 Min. : 3.53
## 1st Qu.:0.0000 1st Qu.:0.00 1st Qu.:15.99
## Median :0.0000 Median :1.00 Median :22.41
## Mean :0.3161 Mean :0.56 Mean :19.62
## 3rd Qu.:1.0000 3rd Qu.:1.00 3rd Qu.:24.00
## Max. :1.0000 Max. :1.00 Max. :24.00
## NA's :7
# stratify table by a factor var
trial %>%
tbl_summary(by = trt, statistic = all_continuous() ~ "m={mean}, sd={sd}") %>%
modify_caption("My Summary Table") %>%
bold_labels() %>%
bold_levels() %>%
add_ci
| Characteristic | Drug A N = 981 |
95% CI | Drug B N = 1021 |
95% CI |
|---|---|---|---|---|
| Age | m=47, sd=15 | 44, 50 | m=47, sd=14 | 45, 50 |
| Unknown | 7 | 4 | ||
| Marker Level (ng/mL) | m=1.02, sd=0.89 | 0.83, 1.2 | m=0.82, sd=0.83 | 0.65, 0.99 |
| Unknown | 6 | 4 | ||
| T Stage | ||||
| T1 | 28 (29%) | 20%, 39% | 25 (25%) | 17%, 34% |
| T2 | 25 (26%) | 17%, 35% | 29 (28%) | 20%, 38% |
| T3 | 22 (22%) | 15%, 32% | 21 (21%) | 13%, 30% |
| T4 | 23 (23%) | 16%, 33% | 27 (26%) | 18%, 36% |
| Grade | ||||
| I | 35 (36%) | 26%, 46% | 33 (32%) | 24%, 42% |
| II | 32 (33%) | 24%, 43% | 36 (35%) | 26%, 45% |
| III | 31 (32%) | 23%, 42% | 33 (32%) | 24%, 42% |
| Tumor Response | 28 (29%) | 21%, 40% | 33 (34%) | 25%, 44% |
| Unknown | 3 | 4 | ||
| Patient Died | 52 (53%) | 43%, 63% | 60 (59%) | 49%, 68% |
| Months to Death/Censor | m=20.2, sd=5.0 | 19, 21 | m=19.0, sd=5.5 | 18, 20 |
| 1 m=Mean, sd=SD; n (%) | ||||
| Abbreviation: CI = Confidence Interval | ||||