Combining multiple CSV files in R

I have about 100 csv files with common headers that I want to combine. The headings are β€œLat,” β€œLong,” and β€œvalue.” I am trying to merge all csv files in such a way that the output will be

"Lat" "Lon" "Value1" "Value2"..."Value 100" 
Columns

Lat and Lon identical for all csv files. Merging two files is easy

 merge(data.frame1, data.frame2, by=c('Lat','Lon')) 

However, I tried the following code that did not work:

 file_list <- list.files(~/source) list_of_files <- lapply(file_list, read.csv) m1 <- merge_all(list_of_files, by=c("Lat","Lon"), all=TRUE) 

which gives an error

 Error in merge.data.frame(dfs[[1]], Recall(dfs[-1]), all = TRUE, sort = FALSE, : formal argument "all" matched by multiple actual arguments. 

Can anyone help me in this regard.

+1
source share
2 answers

You can use Reduce and regular merge :

 m1 <- Reduce(function(old, new) { merge(old, new, by=c('Lat','Lon')) }, list_of_files) 
+4
source

This may work, but you have not given us any data to work with. I personally use the dbaupp method and am not sure which one is faster; However, I rarely get into big data, so the Reduce method is much easier for me to work with this method (I am releasing a new R package in a few months, which has a function to do multimerge, based on the principle of the same thinking, as dbaupp answer). If you are dealing with big data, you may need to point out two (PS, I stole it somewhere, since I rarely think in cycles to solve problems, but I can not indicate where).

 DF <- list_of_files[[1]][, c('lat', 'Lon')] for (.df in list_of_files) { DF <-merge(DF,.df,by=c('Lat', 'Lon'), all=T, suffixes=c("", "")) } DF 
+1
source

All Articles