从 lapply 函数将对象名称分配给不同的 tbl_summary 表

发布于 2025-01-18 20:58:29 字数 562 浏览 2 评论 0原文

我想通过相似的分类变量(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初与友歌 2025-01-25 20:58:29

这是带有您功能的工作版本的Reprex,其中包含代码来设置名称:

library(gtsummary)
dflist <- c("age", "grade")

vartbls <-  lapply(dflist, function(x) {
  tbl_summary_ex2 <-
    trial %>%
    select(age, grade, response, trt) %>%
    tbl_summary(
      by = x,
      label = list(age ~ "Patient Age"),
      statistic = list(all_continuous() ~ "{mean} ({sd})"),
      digits = list(age ~ c(0, 1))
    )
}
)
names(vartbls) <- paste0("tbl_", dflist)

以下是使用{purrr}的版本,并在迭代之前设置名称:

library(gtsummary)
library(purrr)
result <- c("trt", "grade") %>%
  purrr::set_names(paste0("tbl_", .)) %>%
  purrr::map(., ~ trial %>%
    select(age, grade, response, trt) %>%
    tbl_summary(
      by = .x,
      label = list(age ~ "Patient Age"),
      statistic = list(all_continuous() ~ "{mean} ({sd})"),
      digits = list(age ~ c(0, 1))
    ))

Here is a reprex with a working version of your function with code to set the names:

library(gtsummary)
dflist <- c("age", "grade")

vartbls <-  lapply(dflist, function(x) {
  tbl_summary_ex2 <-
    trial %>%
    select(age, grade, response, trt) %>%
    tbl_summary(
      by = x,
      label = list(age ~ "Patient Age"),
      statistic = list(all_continuous() ~ "{mean} ({sd})"),
      digits = list(age ~ c(0, 1))
    )
}
)
names(vartbls) <- paste0("tbl_", dflist)

Here is a version using {purrr} and setting names before iterating:

library(gtsummary)
library(purrr)
result <- c("trt", "grade") %>%
  purrr::set_names(paste0("tbl_", .)) %>%
  purrr::map(., ~ trial %>%
    select(age, grade, response, trt) %>%
    tbl_summary(
      by = .x,
      label = list(age ~ "Patient Age"),
      statistic = list(all_continuous() ~ "{mean} ({sd})"),
      digits = list(age ~ c(0, 1))
    ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文