我使用dplyr
和gtsummary
为列中的多个变量生成一个交叉表,使用以下数据:
# Generate data
set.seed(123)
member <- sample(c("Yes", "No"), 100, replace = TRUE)
author <- sample(c("Yes", "No"), 100, replace = TRUE)
review <- sample(0:10, 100, replace = TRUE)
publish <- sample(0:10, 100, replace = TRUE)
pay <- sample(0:10, 100, replace = TRUE)
data <- data.frame(member, author, review, publish, pay)
我试着使用这个代码,但缺少的是列总数
data %>%
mutate(across(c(review, publish, pay), ~cut(., breaks = c(-Inf, 4.5, 5.5, Inf),
labels = c("No", "Maybe", "Yes"),
include.lowest = TRUE), .names = "{.col}_group")) %>%
select(member, author, ends_with("group")) %>%
tbl_summary(
by = member,
missing = "no",
statistic = list(all_categorical() ~ "{n} ({p}%)"),
digits = list(all_categorical() ~ c(0, 1))
) %>%
add_p(test = all_categorical() ~ "fisher.test")%>%
add_overall() %>%
#add_n() %>%
bold_labels()
我只想为每个变量添加下面的“总体”,“否”和“是”的总和。我尝试了tbl_cross
,但是id不允许在列中有多个变量。
1条答案
按热度按时间tkclm6bt1#
我无法在一个
tbl_summary()
中添加总计行。因此,我为总计行创建了另一个表,并使用
tbl_stack()
合并两个表。创建于2023-05-14带有reprex v2.0.2