Merging multiple data frames in R is anything but straightforward and usually involves confusing loop structures. multiMerge
elegantly avoids such bewildering code chunks by envoking Reduce
upon a list of data frames. The function also features a set of auxiliary parameters that are passed on to merge
. Note that the code is mainly taken from a related blog post in StackOverflow.
## sample data
set.seed(10)
ls_df <- list(data.frame(a = 1:10, b = 1:10),
data.frame(a = 5:14, c = 11:20),
data.frame(a = sample(20, 10), d = runif(10)))
## merge data frames in one go
merge(ls_df, by = "a", all = TRUE)
## a b c d
## 1 1 1 NA NA
## 2 2 2 NA NA
## 3 3 3 NA 0.86472123
## 4 4 4 NA NA
## 5 5 5 11 NA
## 6 6 6 12 0.39879073
## 7 7 7 13 0.05190332
## 8 8 8 14 0.26417767
## 9 9 9 15 0.11350898
## 10 10 10 16 0.59592531
## 11 11 NA 17 0.56773775
## 12 12 NA 18 0.42880942
## 13 13 NA 19 NA
## 14 14 NA 20 NA
## 15 15 NA NA 0.83613414
## 16 16 NA NA 0.35804998