依赖两个文件的编程函数

Programming function that is dependent on two files

我在 R 中有一个函数依赖于两个同名但扩展名不同的文件,例如 a.xxx 和 a.yyy。我需要将这两个文件读入内存来操作它们

我需要此功能适用于包含大约 1000 对文件的文件夹,其中每对文件具有相同的名称,其中一个的扩展名为 .xxx,另一个的扩展名为 .yyy。

我如何对其进行编程,以便用户可以在所有文件中使用某种应用函数,而不是调用该函数 1000 次。 现在我的功能基本上是这样的(伪代码):

sampleFunction<-function(){
     a<-a.xxx
     b<-a.yyy
...
}

如果它只依赖于一个文件,我可以列出该文件夹中的所有文件名并执行 lapply 但是如果函数依赖于两个文件,我该怎么办?

据我了解您的 question/case,这可能会有所帮助:

  sampleFunction<-function(.folderpath, .filename){

    # getting files inside the specific folderpath
    files <- list.files(.folderpath, full.names = T)

    # find matching files
    file_matches <- files[str_detect(files, .filename)]

    # returning both files
    return(file_matches)
   }

当然,如果愿意,您可以读取函数内部的文件:

  sampleFunction<-function(.folderpath, .filename){

    # getting files inside the specific folderpath
    files <- list.files(.folderpath, full.names = T)

    # find matching files
    file_matches <- files[str_detect(files, .filename)]

    # returning both files
    for (i in 1:length(file_matches){

      # for example .RDS files
      readRDS(file_matches[i])
    }
   }

如果您的情况与我的理解略有不同,则此方法可能无效。