Skip to content

Instantly share code, notes, and snippets.

@berndweiss
Created November 6, 2012 10:25
Show Gist options
  • Save berndweiss/4023903 to your computer and use it in GitHub Desktop.
Save berndweiss/4023903 to your computer and use it in GitHub Desktop.
Multiple merge via Reduce()
a <- data.frame(id = c(1,2,3,7,9), y1 = rnorm(5))
b <- data.frame(id = 1:3, y2 = rnorm(3))
c <- data.frame(id = 1:4, y3 = rnorm(4))
a
b
c
Reduce(function(x,y){merge(x, y, by.x = "id", by.y = "id", all = TRUE)},
list(a, b, c), accumulate = FALSE)
## Source: http://rwiki.sciviews.org/doku.php?id=tips:data-frames:merge
## > Reduce(function(x,y){merge(x, y, by.x = "id", by.y = "id", all = TRUE)},
## + list(a, b, c), accumulate = FALSE)
## id y1 y2 y3
## 1 1 0.3262975 0.1469356 0.04394575
## 2 2 -0.4525011 -1.3314288 -0.39520979
## 3 3 0.5057544 -0.5824310 0.50389474
## 4 4 NA NA -1.38640045
## 5 7 0.5549681 NA NA
## 6 9 0.8025466 NA NA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment