r 函数调用,带有 2 个参数列表和 bind_rows 个现有参数

r function call with 2 lists of arguments and bind_rows of existing

我想调用一个带有 2 个参数列表的函数,并且想要 rbind(首选 bind_rows)所有现有的。那是我的通用代码:

combine <- function(arg1, agr2){
  df_gen<-read_csv2(paste0(arg1,"/FolderPref", arg2,"/PID_var.csv")) %>%
  select(PID, Site) %>%
  mutate(Group=arg2)

  dir<-list.files(path = paste0(arg1,"/FolderPref", arg2), pattern = "\General_information.csv$")
  df<-read_csv2(paste0(arg1,"/FolderPref", arg2,"/", dir)) %>%
    filter(Status == 2) %>%
    inner_join(df_gen, by="PID") %>%
    mutate(enrollment=dmy_hm(Date)) %>%
    select(PID, Group, Site, enrollment) 
  return(df)
}

arg1<-list("center1", "center2", "center3", "center4", "center5")
arg2<-list("Group1", "Group2", "Group3", "Group4", "Group5")

我的最终目标是拥有一个包含 arg1 (center1-group1, center1-group2,... center2-group1, center2-group2,... )所有可能组合的 df

问题是并非所有 "General_Information.csv" 文件都存在。因此,从 5x5 可能的组合中,最终数据帧将具有少于 25 个单个 df。

当然我可以写 25 行 "combine" 并注释 "bind_rows" 调用中不存在的 df。但是我知道这个问题会有更好的解决方案。

期待您的支持。

这是我通常做的事情:

# Make all possible combinations of your arguments
argExpand <- expand.grid(arg1, arg2)
head(argExpand)

#     Var1   Var2
# 1 center1 Group1
# 2 center2 Group1
# 3 center3 Group1
# 4 center4 Group1
# 5 center5 Group1
# 6 center1 Group2

# Iterate using foreach
library(foreach)
foreach(i = 1:nrow(argExpand), .combine = rbind) %do% {
   combine(argExpand[i, 1], argExpand[i, 2])
}

PS.: 要处理可能不存在的文件,请使用 file.exists():

file <- paste0(arg1,"/FolderPref", arg2,"/PID_var.csv")
if (file.exists(file)) {
    df_gen <- read_csv2(file) %>%
        select(PID, Site) %>%
        mutate(Group = arg2)
}