Read all files in directory and apply multiple functions to each data frame [duplicate]

You can get all the files and then loop using lapply and apply whatever function you want to apply as follows:

files <- list.files(path="path/to/dir", pattern="*.txt", full.names=TRUE, recursive=FALSE)
lapply(files, function(x) {
    t <- read.table(x, header=TRUE) # load file
    # apply function
    out <- function(t)
    # write to file
    write.table(out, "path/to/output", sep="\t", quote=FALSE, row.names=FALSE, col.names=TRUE)
})

Leave a Comment