我使用 gtsummary 的 tbl_summary 制作了调查问题的汇总表,其中答案有 5 或 7 个级别同意、不同意等,但表格很长。有什么方法可以制作表格,其中答案的级别位于列中?
这是我的代码:
table4<-physician[ ,32:43] %>% tbl_summary() %>%
modify_footnote(all_stat_cols()~"Frequency (%)") %>%
modify_caption("** Table 7. The frequency with which prescribers provided antibiotics or resources related to prudent use of antibiotics,
behavior on drivers for initiating prescriptions and antibiotic prescribing behaviors **") %>%
modify_header(label~"Item") %>% bold_labels()
我希望我的桌子看起来像这样:
Item SA A D SD N/A U IDU
quesion 56 47 35 89 15 28 10
问题在行中,每个级别的级别和计数在冒号中
它有助于提供示例数据。如果没有它,我在下面创建了一个简短的示例:
set.seed(44)
df <- data.frame(
question1 = sample(c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree", NA), 15, replace = TRUE),
question2 = sample(c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree", NA), 15, replace = TRUE),
question3 = sample(c("Strongly Agree", "Agree", "Disagree", "Strongly Disagree", NA), 15, replace = TRUE)
)
这里每个问题都有一栏,每一栏都包含一个 Likert 类型的答案。
question1 question2 question3
1 Strongly Agree <NA> Strongly Agree
2 Disagree Strongly Disagree <NA>
3 Disagree <NA> <NA>
4 Strongly Agree <NA> Agree
5 Strongly Disagree Strongly Agree Agree
6 Strongly Agree Strongly Agree Disagree
7 <NA> Agree Strongly Disagree
8 Strongly Agree <NA> <NA>
9 Strongly Agree <NA> Disagree
10 Agree <NA> Agree
11 Agree Strongly Agree Strongly Agree
12 <NA> Strongly Agree Strongly Agree
13 Strongly Agree Strongly Disagree Disagree
14 Strongly Agree Agree Agree
15 Strongly Agree Disagree Strongly Disagree
这可能是
bstfun
包的理想选择,它可以创建 gtsummary
类似李克特型量表的表格。目前CRAN上还没有,但是你可以下载开发版本(我注释掉了)。
# devtools::install_github("MSKCC-Epi-Bio/bstfun")
library(bstfun)
tbl_likert(df)
输出
或者,您可以单独使用
gtsummary
。您可以创建单独的表格行,然后将它们堆叠起来以创建完整的表格。您也可以轻松指定缺失的列。
library(tidyverse)
library(gtsummary)
map(
names(df),
~ df |>
select(all_of(.x)) |>
mutate(across(all_of(.x), ~fct_na_value_to_level(., "Missing")),
label_char = TRUE) |>
tbl_summary(
by = all_of(.x),
statistic = ~"{n}",
missing = "no",
label = list(label_char = .x)
) |>
modify_header(
all_stat_cols() ~ "**{level}**",
label = "**Question**"
)
) |>
tbl_stack()
输出