I would also recommend gtsummary (written by Daniel D. Sjoberg et al). You can generate publication-ready or presentation-ready tables with the package. A gtsummary solution to the example given in the question would be:
library(tidyverse)
library(gtsummary)
data <- c(62, 60, 63, 59, 63, 67, 71, 64, 65, 66, 68, 66,
71, 67, 68, 68, 56, 62, 60, 61, 63, 64, 63, 59)
grp <- factor(rep(LETTERS[1:4], c(4,6,6,8)))
df <- data.frame(group=grp, dt=data)
tbl_summary(df,
by=group,
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{mean} ({sd})","{median} ({IQR})", "{min}- {max}"), ) %>%
add_stat_label(label = dt ~ c("Mean (SD)","Median (Inter Quant. Range)", "Min- Max"))
which then gives you the output below
Characteristic |
A, N = 4 |
B, N = 6 |
C, N = 6 |
D, N = 8 |
dt |
|
|
|
|
Mean (SD) |
61.0 (1.8) |
66.0 (2.8) |
68.0 (1.7) |
61.0 (2.6) |
Meian (IQR) |
61.0 (2.5) |
65.5 (2.5) |
68.0 (0.8) |
61.5 (3.2) |
Min- Max |
59.0 - 63.0 |
63.0 - 71.0 |
66.0 - 71.0 |
56.0 - 64.0 |
You can also export the table as word document by doing the following:
Table1 <- tbl_summary(df,
by=group,
type = all_continuous() ~ "continuous2",
statistic = all_continuous() ~ c("{mean} ({sd})","{median} ({IQR})", "{min}- {max}"), ) %>%
add_stat_label(label = dt ~ c("Mean (SD)","Median (Inter Quant. Range)", "Min- Max"))
tmp1 <- "~path/name.docx"
Table1 %>%
as_flex_table() %>%
flextable::save_as_docx(path=tmp1)
You can use it for regression outputs as well. See the package reference manual and the package webpage for further insights
https://cran.r-project.org/web/packages/gtsummary/index.html
https://www.danieldsjoberg.com/gtsummary/index.html
data <- c(
line. – Colicweed