0

I need to read multiple .csv files into one variable in R. All the files contain data with the same headings but I want the variable in R to have the headings only once, and then all the data from all the .csv files in the rows beneath. At the moment I have this:

All_Data <- list()
List_Data <- dir(pattern = ".csv")
for (i in 1:length(List_Data)){
    All_Data[[i]] <- read.csv(List_Data[i])
}

Which works to combine the files, but means I have the headings from every file in rows in between data which I do not want.

  • Have you try to reed the first and then in the loop put header=FALSE ? – Santiago I. Hurtado Nov 30 '18 at 12:13
  • @SantiagoI.Hurtado so read the first .csv file outside of the loop and then in the line in the loop add header = FALSE? That sounds like it should work, thank you! – parzemis Nov 30 '18 at 12:18
  • @SantiagoI.Hurtado: parzemis seems to misunderstand the format/class of `All_Data`. It's a list of date.frames, with each data.frame having the same headers/column names, not a single data frame with headers interspersed. – AkselA Nov 30 '18 at 12:39
  • 1
    @parzemis: No, leave the loop as is. Just do `do.call(rbind, All_Data)` after the loop has run, and you should be fine. – AkselA Nov 30 '18 at 12:40
  • @AkselA's solution is best. When you `rbind` multiple data frames with the same column names, all the rows are stacked on top of each other. – qdread Nov 30 '18 at 13:49

0 Answers0