且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

循环遍历R中目录中的所有文件,应用多个命令

更新时间:2023-09-17 16:20:10

您可以获取所有文件,然后循环使用 lapply 并应用您想要的任何功能应用如下:

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=T, recursive=FALSE)
lapply(files, function(x) {
    t <- read.table(x, header=T) # load file
    # apply function
    out <- function(t)
    # write to file
    write.table(out, "path/to/output", sep="\t", quote=F, row.names=F, col.names=T)
})