rfilecsvappend

How to append multiple files in R


I'm trying to read a list of files and append them into a new file with all the records. I do not intend to change anything in the original files. I've tried couple of methods.

Method 1: This methods creates a new file but at each iteration the previous file gets added again. Because I'm binding the data frame recursively.

files <- list.files(pattern = "\\.csv$")

  #temparary data frame to load the contents on the current file
  temp_df <- data.frame(ModelName = character(), Object = character(),stringsAsFactors = F)

  #reading each file within the range and append them to create one file
  for (i in 1:length(files)){
    #read the file
    currentFile = read.csv(files[i])

    #Append the current file
    temp_df = rbind(temp_df, currentFile)    
  }

  #writing the appended file  
  write.csv(temp_df,"Models_appended.csv",row.names = F,quote = F)

Method 2: I got this method from Rbloggers . This methods won't write to a new file but keeps on modifying the original file.

multmerge = function(){
  filenames= list.files(pattern = "\\.csv$")
  datalist = lapply(filenames, function(x){read.csv(file=x,header=T)})
  Reduce(function(x,y) {merge(x,y)}, temp_df)

}

Can someone advice me on how to achieve my goal?


Solution

  • it could look like this:

    files <- list.files(pattern = "\\.csv$")
    
    DF <-  read.csv(files[1])
    
    #reading each file within the range and append them to create one file
    for (f in files[-1]){
      df <- read.csv(f)      # read the file
      DF <- rbind(DF, df)    # append the current file
    }
    #writing the appended file  
    write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)
    

    or short:

    files <- list.files(pattern = "\\.csv$")
    
    DF <-  read.csv(files[1])
    for (f in files[-1]) DF <- rbind(DF, read.csv(f))   
    write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)