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