将文件读入 R,然后对它们应用相同的函数,然后 return 一个结果文件

Read in files to R then apply the same function to them and return a results file

我有超过 100 个具有相同列的文件。我使用以下代码将它们读入 R:

# packages
require(data.table)
# set wd
setwd("PathToYourFolder")
# import files
files = list.files(pattern="*.txt.results")

我想对所有这些文件应用以下功能:

fdr <- p.adjust (file$col4, method = "fdr", n = length(file$col4)) 

我尝试了以下方法:

lapply(files, p.adjust (files$col4, method = "fdr", n = length (files$col4)))

你能告诉我怎么做吗?

嗯,你很接近。


your.p.values <- lapply(files, function(file) {

    dat <- read.csv( file )
    
    p.adjust (dat$col4, method = "fdr", n = length (dat$col4)))

})

## add the filenames to them to keep better order of things:
names(your.p.values) <- files

purrr包中有一个函数可以帮你将文件名添加到列表中,省了一步,代码也少了一些,但上面的解决方案应该没问题为此。