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

I have more than 100 files with the same columns. I read them into R with the following code:

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

I want to apply the following function to all these files:

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

I tried the following:

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

Can you advise me how to do this?

1 answer

  • answered 2021-04-14 22:00 Sirius

    Well you were close.

    
    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
    
    

    There is a function in the purrr package that will add the names of the files to the list for you, saving you one step and stripping down the code somewhat, but the above solution should be just fine for this.