【问题标题】:R: loop and operation on file name in a directoryR:对目录中的文件名进行循环和操作
【发布时间】:2018-06-15 13:02:09
【问题描述】:

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

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=";")

所以,这里的变化是:“模式”中的名称和要保存在 write.table 中的名称。

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

【问题讨论】:

  • 例如,只需将您的模式名称放在一个向量中,然后使用lapply 循环遍历它们。

标签: r loops filenames


【解决方案1】:

类似这样的:

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=";")
}

更新:(至 2end 评论)你的意思是这样的吗?

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){

  }
}

?

【讨论】:

  • 我删除了我的答案,因为它几乎相同。请注意,在这种情况下,在 for 循环中执行此操作非常好,因为每次迭代都不会构建任何对象。
  • 在我喜欢这 4 个网站的情况下,这是一个非常好的答案。它有效,但我想为同一目录中的数百个站点名称执行此操作,因此将名称放入 runPat 迭代需要时间。一个文件的标准名称是:CC-Sss.yyyy.synth.hourly.allvars.csv。 CC 代表国家(如示例 CH、FR...大写字母),sss 代表站点(如示例:Fon、Lq2),sss 可以是大写也可以不是数字。 yyyy 代表年份。我们可以根据之前的答案改进这一点吗?
  • 您可以使用dir 函数来获取目录中的所有文件和子文件夹,然后使用例如regexp 函数获取您正在寻找的模式
  • @HolgerBarlt:在回答您的更新时,这看起来也不错,但我想避免创建一个所有名称为 c("CH", "FR"...) 的向量,因为实际上,我有一百个……所以太长了。你是对的,我应该使用 dir 和 regexp 的东西......我正在尝试,但不是很熟悉!
  • 查看最新答案更新?如果您不提供有关文件系统结构的更多详细信息,则很难提供更详细的答案...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 1970-01-01
  • 1970-01-01
  • 2013-09-20
  • 1970-01-01
  • 2020-01-16
相关资源
最近更新 更多