将应用文件列表用于 R 中的函数

use apply file list for a function in R

我创建这个函数是为了从两个不同的文件中获取一些信息

   create_distance_to_strand <- function(data, data2, nameCsv) {
      newData  <- data  %>% 
        select(seqnames, start, end, V4, seqnames, geneStart, geneEnd, geneStrand, distanceToTSS, SYMBOL, geneLength)%>%
        rename(peak = V4)
      
      joined_df <- merge(newData , closest_dist, by.x = "peak", 
                         by.y = "peak", all.x = TRUE, all.y = TRUE) %>% drop_na()
      
      write.table(joined_df , nameCsv, sep = "\t")
    
      
      return(joined_df)
    }
    
    closest_dist = read.csv("closest_distance", header = T, sep ="\t")
    annotation = read.csv("results_annotation", header = T, sep ="\t") 
    myDistanceToStrand <- create_distance_to_strand(annotation,  closest_dist, "frame.csv")

它按预期工作。但是,我试图提高它的效率,以防我有不同的 "closest_dist" 文件,以便将函数应用于所有文件。 我试过这个:

    files <- list.files(pattern = "closest*")
    proof = lapply(files, create_distance_to_strand(annotation,  closest_dist, "proof.csv"))

但不起作用

Error in h(simpleError(msg, call)) : 
      error in evaluating the argument 'y' in selecting a method for function 'merge': object 'closest_dist' not found 

有什么建议吗? 谢谢

由于您有不同的 closest_dist 文件保存在 files 中,您可以使用 lapply 将它们一一传递。我们可以在这里使用匿名函数 -

files <- list.files(pattern = "closest*")
proof = lapply(files, function(x) create_distance_to_strand(annotation,  x, "proof.csv"))

要有一个单独的输出文件,您也可以传递不同的 nameCsv 值。

Map(function(x, y) create_distance_to_strand(annotation, x, y), 
    files, sprintf('proof%d.csv', seq_along(files)))