Skip to content

Instantly share code, notes, and snippets.

@egeulgen
Last active October 23, 2019 12:45
Show Gist options
  • Save egeulgen/48b10331eb85791285cae9a08f279557 to your computer and use it in GitHub Desktop.
Save egeulgen/48b10331eb85791285cae9a08f279557 to your computer and use it in GitHub Desktop.
Merges multiple files into a single dataframe
multimerge <- function(mypath){
filenames <- list.files(path=mypath, full.names=TRUE)
datalist <- lapply(filenames, function(x) read.csv(file=x,header=T))
result_df <- Reduce(function(x,y) merge(x,y), datalist)
return(result_df)
}
### Cleaner and faster
# import files
files <- list.files(pattern="*.csv")
dataset <- do.call(rbind, lapply(files, data.table::fread))
# transform data to df
dataset <- as.data.frame(unclass(dataset))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment