Search code examples
rloopsfilenames

R: loop and operation on file name in a directory


I have a directory in which I have several files for several sites and years, with exactly the same internal structure. I want to merge (rbind) each files that are from the same sites. Here is the code that is working for one site (define in "pattern"), but instead of doing this manualy for each site name, I want to create a loop that is doing this for all the site.

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

So, what is changing here is : the name in "pattern" and the name to save in write.table.

Any idea to simplify this? Many thanks in advance


Solution

  • Something like this:

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

    Update: (to 2end comment) you mean something like this?

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

    how about replacing the two Loops above by

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

    ?