我还建议使用gtsummary(由Daniel D. Sjoberg等人编写)。您可以使用该软件包生成出版物或演示文稿所需的表格。在此问题中,使用gtsummary解决方案如下:
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"))
然后您就会得到以下输出结果:
特征 |
A,N = 4 |
B,N = 6 |
C,N = 6 |
D,N = 8 |
dt |
|
|
|
|
平均值(标准差) |
61.0(1.8) |
66.0(2.8) |
68.0(1.7) |
61.0(2.6) |
中位数(四分位距) |
61.0(2.5) |
65.5(2.5) |
68.0(0.8) |
61.5(3.2) |
最小值-最大值 |
59.0 - 63.0 |
63.0 - 71.0 |
66.0 - 71.0 |
56.0 - 64.0 |
您还可以通过以下方式将表格导出为Word文档:
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)
您可以将其用于回归输出。有关更多信息,请参阅软件包参考手册和软件包网页。
https://cran.r-project.org/web/packages/gtsummary/index.html
https://www.danieldsjoberg.com/gtsummary/index.html
data <- c(
行末有多余逗号。 - BenBarnes