这是一个跟进问题
请考虑以下示例:
library(gtsummary)
library(gtreg)
library(dplyr)
library(tidyr)
df <- structure(
list(id = c("patient1", "patient2", "patient3", "patient4",
"patient5"), x = c("h,a,a", "i", "i", "i,a,e", "h")),
class = "data.frame", row.names = c(NA, -5L)
)
df1 <- structure(
list(id = c("patient1", "patient2", "patient3", "patient4",
"patient5"), y = c("yes", "no", "yes", "yes", "no")),
class = "data.frame", row.names = c(NA, -5L)
)
# prep data for a table
tbl <- df |>
separate_rows(x, sep = ",") |>
select(x) |>
count(x) |>
mutate(percent = round(n/nrow(df)*100)) |>
mutate(N_percent = paste0(n,"/", nrow(df), " (",percent,"%)"), .keep = "unused") |>
select(x, N = N_percent) |>
mutate(grouping_var = "x", .before = 1L) |>
# create a gtsummary class listing
tbl_listing(df, group_by = grouping_var) |>
# style table with gtusmmary functions to make it look like `tbl_summary()`
modify_header(
x ~ "**Characteristic**",
N ~ "**N = 5**"
) |>
modify_column_alignment(N, align = "center") |>
modify_footnote(N = "n/N (%)")
tbl1 <- df1 |>
select(y) |>
tbl_summary(
type = list(y ~ "categorical"))
tbl_stack(
tbls = list(tbl, tbl1)
)
字符串
在第二个表中产生移位:
的数据
我会期待这样的事情:
的
2条答案
按热度按时间juud5qan1#
正如@丹尼尔D. Sjoberg的宝贵评论(所有功劳都归功于他):
1.用
gtreg
sshow_header_names()
检查头名称:字符串
现在在
tbl
中重命名相应的名称:型
x1c 0d1x的数据
f3temu5u2#
在我的answer的基础上,你可以使用
tbl_custom_summary
来实现你想要的结果。这里是使用我的自定义函数的一个通用版本,它也允许传递标识符列的名称,并通过添加distinct
参数来处理这两种类型的变量。字符串
的数据