Frage

I have a folder that looks like this

> list.files()
[1] "2013-09-13(1).csv" "2013-09-13.csv"    "2013-09-14.csv"    "2013-09-15.csv"   
[5] "2013-09-16.csv"    "2013-09-17.csv"    "2013-09-18.csv"    "2013-09-19.csv"   
[9] "2013-09-20.csv"    "2013-09-21.csv"    "2013-09-22.csv"    "2013-09-23.csv"   
[13] "2013-09-24.csv"    "2013-09-25.csv"    "2013-09-26(1).csv" "2013-09-26(2).csv"
[17] "2013-09-26.csv"    "2013-09-27.csv"    "2013-09-28.csv"    "2013-09-29.csv"   
[21] "2013-09-30.csv"   

As can be seen, there are files with the same names, such as "2013-09-13(1).csv" & "2013-09-13.csv". What I want to do is create some form of loop that can detect such problems and rbind the files

2013-09-03 <- rbind(2013-09-13.csv, 2013-09-13(1).csv)
write.csv(2013-09-03, file="2013-09-03.csv", row.names=FALSE)
2013-09-26 <- rbind(2013-09-26.csv, 2013-09-26(1).csv, 2013-09-26(2).csv)
write.csv(2013-09-26, file="2013-09-26.csv", row.names=FALSE)

So at the end, I can see this:

list.files()
[1] "2013-09-13.csv"    "2013-09-14.csv"    "2013-09-15.csv"   "2013-09-16.csv"
[5] "2013-09-17.csv"    "2013-09-18.csv"    "2013-09-19.csv"   "2013-09-20.csv" 
[9] "2013-09-21.csv"    "2013-09-22.csv"    "2013-09-23.csv"   "2013-09-24.csv"
[13] "2013-09-25.csv"    "2013-09-26.csv"    "2013-09-27.csv"    "2013-09-28.csv"
[17] "2013-09-29.csv"    "2013-09-30.csv"

Does anyone know how I can do this?

War es hilfreich?

Lösung

Soemthing along these lines should work, haven't run it myself.

library(data.table)
patternstomatch <- gsub(list.files(), pattern = ".csv", replacement = "")

for (i in patternstomatch)
{
  filestorbind <- list.files(pattern = i)
  if( length(filestorbind ) > 1)
  {
    rbindeddataset <- rbindlist(lapply(filestorbind, read.csv))
    write.csv(rbindeddataset , file = paste(i,".csv", sep = ""))
  }
}

patternstomatch will contain "2013-09-13(1)","2013-09-13","2013-09-14", etc. the loop will look for each of these strings in the filenames. If multiple files are found matching a pattern it will read them all, rbind them and write it out as ".csv".

Using the gsub approach on the first line, you can then find files that have "(" in their file name and use file.remove to delete those files.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top