0

I want to read multiple .csv files in a directory(a folder in the working directory) but each time a new file is read the data is overwriten to the dataframe. I want the data from the new to be appended to the end.

my code

library(dplyr)
pollutantMean <- function(directory, pollutant, id) {
files <- list.files(directory)
files <- paste(directory,files, sep = "/")
for (i in id) {
    df <- read_csv(files[i])
}
}

Tthe function is called as pollutantMean("nameOfDirectory", "sulfate", 1:20)

I found a way to do this with the plyr package using:

files <- list.files(directory)
files <- paste(directory,files, sep = "/")
df <- ldply(files, read_csv)

Looking for the fastest method

bond007
  • 23
  • 3
  • I believe this has already been answered - please see here: https://stackoverflow.com/questions/23995384/read-and-rbind-multiple-csv-files – gabesolomon10 Apr 28 '21 at 15:32
  • I suggest that "fastest" is not always better than "readable and maintainable", but that's relative to the programmer using the code. In this case, since you're already loading `dplyr`, I suggest `bind_rows(lapply(files, readr::read_csv))` is the most direct and as memory-efficient as can be within R. Another variant is from `data.table`, with `rbindlist(lapply(files, fread))`, same end-effect. – r2evans Apr 28 '21 at 15:43

0 Answers0