0

I have 3 data frames of unequal sizes which I would like to merge together (by row) according to their column labels.

a = matrix(c(1,2,3,4,5,6),nrow=2,ncol=3)
b = matrix(c(1,3,4,5,6,7,8,9),nrow=2,ncol=4)
c = matrix(c(2,4,4,5,6,7),nrow=2,ncol=3)
a = data.frame(a)
b = data.frame(b)
c = data.frame(c) 
colnames(a) <- c('Apples','Pears','Oranges')
colnames(b) <- c('Apples','Oranges','Peaches','Pears')
colnames(c) <- c('Apples','Pears','Peaches')

So I'm after a merged matrix of 6 rows x 4 columns with all the Apple, Pear, Orange and Peach data in the same columns.

How do I do this?

user2846211
  • 949
  • 6
  • 16
  • 24
  • Duplicate - http://stackoverflow.com/questions/1299871/how-to-join-data-frames-in-r-inner-outer-left-right – Tom Ron Apr 25 '14 at 13:48

3 Answers3

1

Is this what you want?

library(plyr)
rbind.fill(a, b, c)
#  Apples Pears Oranges Peaches
# 1      1     3       5      NA
# 2      2     4       6      NA
# 3      1     8       4       6
# 4      3     9       5       7
# 5      2     4      NA       6
# 6      4     5      NA       7
Henrik
  • 65,555
  • 14
  • 143
  • 159
1
Reduce(function(...) merge(..., all=TRUE), list(a,b,c))
#   Apples Pears Peaches Oranges
# 1      1     3      NA       5
# 2      1     8       6       4
# 3      2     4       6      NA
# 4      2     4      NA       6
# 5      3     9       7       5
# 6      4     5       7      NA
user1197460
  • 108
  • 6
1

Also using plyr:

> library(plyr)
> join_all(list(a,b,c), type = "full")
Joining by: Apples, Pears, Oranges
Joining by: Apples, Pears, Peaches
  Apples Pears Oranges Peaches
1      1     3       5      NA
2      2     4       6      NA
3      1     8       4       6
4      3     9       5       7
5      2     4      NA       6
6      4     5      NA       7
pak
  • 855
  • 8
  • 20