从 lapply 函数将对象名称分配给不同的 tbl_summary 表
我想通过相似的分类变量(var1,var2,var3)应用于 “ by =” ,并分配每个对象名称eg“ tbl_var1) ”,“ tbl_var2”和“ tbl_var3”
dflist <- c("var1",
"var2",
"var3")
vartbls = lapply(dflist, function(df) {
tbl_summary_ex2 <-
trial %>%
select(age, grade, response, trt) %>%
tbl_summary(
by = df,
label = list(age ~ "Patient Age"),
statistic = list(all_continuous() ~ "{mean} ({sd})"),
digits = list(age ~ c(0, 1))
)
}
)
I would want to generate different tbl_summary tables from a loop(lapply function) over similar categorical variables (var1, var2, var3) applied to "by= " and assign each of them an object name e.g "tbl_var1", "tbl_var2" and "tbl_var3"
dflist <- c("var1",
"var2",
"var3")
vartbls = lapply(dflist, function(df) {
tbl_summary_ex2 <-
trial %>%
select(age, grade, response, trt) %>%
tbl_summary(
by = df,
label = list(age ~ "Patient Age"),
statistic = list(all_continuous() ~ "{mean} ({sd})"),
digits = list(age ~ c(0, 1))
)
}
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是带有您功能的工作版本的Reprex,其中包含代码来设置名称:
以下是使用{purrr}的版本,并在迭代之前设置名称:
Here is a reprex with a working version of your function with code to set the names:
Here is a version using {purrr} and setting names before iterating: