Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append multiple files in R

Tags:

file

append

r

csv

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?

like image 412
SriniShine Avatar asked Nov 06 '15 10:11

SriniShine


2 Answers

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)
like image 116
jogo Avatar answered Oct 18 '22 01:10

jogo


You can use this to load everything into one data set.

dataset <- do.call("rbind", lapply(file.list, FUN = function(file) {
  read.table(file, header=TRUE, sep="\t")
}))

And then just save with write.csv.

like image 22
m0nhawk Avatar answered Oct 18 '22 01:10

m0nhawk