Reputation: 223
So I'm very new at R and right now I'm trying to load multiple .csv files (~60 or so) and then merge them together. They all have similar columns and their files are named like this: dem_file_30, dem_file_31.
I've been trying to use scripts online but keep getting some errors. I'm sure I can do it by hand but that would be really tedious.
Example:
file_list <- list.files("/home/sjclark/demographics/")
list_of_files <- lapply(file_list, read.csv)
m1 <- merge_all(list_of_files, all=TRUE)
Error: merge_all doesn't exist
This one seems to read them into R, but then I'm not how to do after that... help?
setwd("/home/sjclark/demographics/")
filenames <- list.files(full.names=TRUE)
All <- lapply(filenames,function(i){
read.csv(i, header=TRUE)
})
Upvotes: 3
Views: 8970
Reputation: 851
I have just been doing a very similar task and was also wondering if there is a faster/better way to do it using dplyr
and bind_rows
.
My code for this task is uses ldply
from plyr
:
library(plyr)
filenames <- list.files(path = "mypath", pattern = "*", full.names=TRUE)
import.list <- ldply(filenames, read.csv)
Hope that helps
Rory
Upvotes: 4
Reputation: 2336
It appears as if you might be trying to use the nice function shared on R-bloggers (credit to Tony Cookson):
multMerge = function(mypath){
filenames = list.files(path = mypath, full.names = TRUE)
datalist = lapply(filenames,
function(x){read.csv(file = x,
header = TRUE,
stringsAsFactors = FALSE)})
Reduce(function(x,y) {merge(x, y, all = TRUE)}, datalist)
}
Or perhaps you have pieced things together from difference sources? In any event, merge
is the crucial base R function that you were missing. merge_all
doesn't exist in any package.
Since you're new to R (and maybe all programming) it's worth noting that you'll need to define this function before you use it. Once you've done that you can call it like any other function:
my_data <- multMerge("/home/sjclark/demographics/")
Upvotes: 6