R:对目录中的文件名进行循环和操作

R: loop and operation on file name in a directory

我有一个目录,其中包含多个站点和年份的多个文件,内部结构完全相同。我想合并(rbind)来自相同站点的每个文件。 这是适用于一个站点的代码(在 "pattern" 中定义),但我不想为每个站点名称手动执行此操作,而是想创建一个循环来为所有站点执行此操作。

ls1 <- list.files(path = dossier, pattern = "CH", all.files = FALSE,
              full.names = FALSE, recursive = FALSE,
              ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
ld1 <- lapply(ls1, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
tot1 <- do.call("rbind", ld1)
write.table(tot1, file=file.path(dossier,"CH-Oe1_2002-2006.csv"),col.names=TRUE, row.names=FALSE, sep=";")

ls2 <- list.files(path = dossier, pattern = "FR-Fon", all.files = FALSE,
              full.names = FALSE, recursive = FALSE,
              ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
ld2 <- lapply(ls2, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
tot2 <- do.call("rbind", ld2)
write.table(tot2, file=file.path(dossier,"FR-Fon_2002-2006.csv"),col.names=TRUE, row.names=FALSE, sep=";")

ls3 <- list.files(path = dossier, pattern = "FR-Gri", all.files = FALSE,
              full.names = FALSE, recursive = FALSE,
              ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
ld3 <- lapply(ls3, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
tot3 <- do.call("rbind", ld3)
write.table(tot3, file=file.path(dossier,"FR-Gri_2002-2006.csv"),col.names=TRUE, row.names=FALSE, sep=";")

ls4 <- list.files(path = dossier, pattern = "FR-Lq2", all.files = FALSE,
              full.names = FALSE, recursive = FALSE,
              ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
ld4 <- lapply(ls4, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
tot4 <- do.call("rbind", ld4)
write.table(tot4, file=file.path(dossier,"FR-Lq2_2002-2006.csv"),col.names=TRUE, row.names=FALSE, sep=";")

因此,这里要更改的是:"pattern" 中的名称和 write.table 中要保存的名称。

有什么办法可以简化这个吗? 非常感谢

像这样:

for(runPat in c("CH","FR-Fon","FR-Gri","FR-Lq2"))  
  ls <- list.files(path = dossier, pattern = runPat, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
  ld <- lapply(ls, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
  tot <- do.call("rbind", ld)
  write.table(tot2, file=file.path(dossier,sprintf("%s_2002-2006.csv",runPat)),col.names=TRUE, row.names=FALSE, sep=";")
}

更新:(到第 2 条评论结束)你的意思是这样的?

allCont  <- c("CH", "FR")
allSites <- c("", "Fon", "Gri", "Lq2")

for(runCont in allCont){
  for(runSize in allSites){
    if(length(runSite)>0){
      runPat <- sprintf("%s-%s",runCont,runSite)
    }else{
      runPat <- runCont
    }
    ls <- list.files(path = dossier, pattern = runPat, all.files = FALSE, full.names = FALSE, recursive = FALSE, ignore.case = FALSE, include.dirs = FALSE, no.. = FALSE)
    ld <- lapply(ls, function(x) read.csv(file=file.path(dossier,x), sep=",", header=T, stringsAsFactors=FALSE))
    tot <- do.call("rbind", ld)
    write.table(tot2, file=file.path(dossier,sprintf("%s_2002-2006.csv",runPat)),col.names=TRUE, row.names=FALSE, sep=";")
  }

} 

如何将上面的两个循环替换为

allCNT <- dir()
for (runCnt in allCNT){
  allSites <- dir(runCnt)
  for (runSite in allSites){

  }
}

?