0

I am quite new to R and I need some help. I have multiple csv files labeled from 001 to 332. I would like to combine all of them into one data.frame. This is what I have done so far:

filesCSV <- function(id = 1:332){

  fileNames <- paste(id)   ## I want fileNames to be a vector with the names of all the csv files that I want to join together
    for(i in id){
       if(i < 10){
       fileNames[i] <- paste("00",fileNames[i],".csv",sep = "")

}
       if(i < 100 && i > 9){
       fileNames[i] <- paste("0", fileNames[i],".csv", sep = "")

}
else if (i > 99){
  fileNames[i] <- paste(fileNames[i], ".csv", sep = "")
}

}
     theData <- read.csv(fileNames[1], header = TRUE)    ## here I was trying to create the data.frame with the first csv file
     for(a in 2:332){
      theData <- rbind(theData,read.csv(fileNames[a]))    ## here I wanted to use a for loop to cycle through the names of files in the fileNames vector, and open them all and add them to the 'theData' data.frame


}
   theData
}

Any help would be appreciated, Thanks!

mutaween464
  • 253
  • 2
  • 8
  • instead of using for loop it would be better to use `list.files`, the pattern argument will be handy in defining a regular expression for filenames – Imran Ali Aug 07 '17 at 04:38
  • @qqq This is not a good dupe target as it deals with column binding or merging of many files while the actual question is about row binding. – Uwe Aug 07 '17 at 05:09

2 Answers2

1

Hmm it looks roughly like your function should already be working. What is the issue? Anyways here would be a more idiomatic R way to achieve what you want to do that reduces the whole function to three lines of code:

Construct the filenames:

infiles <- sprintf("%03d.csv", 1:300)

the %03d means: insert an integer value d padded to length 3 zeroes (0). Refer to the help of ?sprintf() for details.

Read the files:

res <- lapply(infiles, read.csv, header = TRUE)

lapply maps the function read.csv with the argument header = TRUE to each element of the vector "infiles" and returns a list (in this case a list of data.frames)

Bind the data frames together:

do.call(rbind, res)

This is the same as entering rbind(df1, df2, df3, df4, ..., dfn) where df1...dfn are the elments of the list res

Stefan F
  • 2,573
  • 1
  • 17
  • 19
0

You were very close; just needed ideas to append 0s to files and cater for cases when the final data should just read the csv or be an rbind

filesCSV <- function(id = 1:332){
  library(stringr) 
  # Append 0 ids in front
  fileNames <- paste(str_pad(id, 3, pad = "0"),".csv", sep = "")

  # Initially the data is NULL
  the_data <- NULL
  for(i in seq_along(id)
  {
    # Read the data in dat object
    dat <- read.csv(fileNames[i], header = TRUE)
    if(is.null(the_data) # For the first pass when dat is NULL
    {
        the_data <- dat
    }else{ # For all other passes
         theData <- rbind(theData, dat)
    }
  }  
   return(the_data)
}
discipulus
  • 2,665
  • 3
  • 34
  • 51