Dear all, I have two data.frames a and b: i <- c(1,1,2,2,3,3,4,4) x <- c(1,53,7,3,4,23,6,2) a <- data.frame(i,x) and j <- c(1,2,3,4) y <- c(99,88,77,66) b <- data.frame(j,y) So, a looks like this> ai x 1 1 1 53 2 7 2 3 3 4 3 23 4 6 4 2 and b like this> bj y 1 99 2 88 3 77 4 66 Now, I would like to match 'b' to 'a', so that a new data.frame 'c' is> ci x z 1 1 1 99 2 1 53 99 3 2 7 88 4 2 3 88 5 3 4 77 6 3 23 77 7 4 6 66 8 4 2 66 I habe absolutely no idea how to do this. I searched the net, the FAQ, the manuals, my four books... Any help would be appreciated! Bernd
Philippe Glaziou
2003-Nov-05 11:47 UTC
[R] Match data.frames with different number of rows
Bernd Weiss <bernd.weiss at uni-koeln.de> wrote:> I have two data.frames a and b: > > i <- c(1,1,2,2,3,3,4,4) > x <- c(1,53,7,3,4,23,6,2) > a <- data.frame(i,x) > > and > > j <- c(1,2,3,4) > y <- c(99,88,77,66) > b <- data.frame(j,y) >Now, I would like to match 'b' to 'a', so that a new data.frame 'c' is> > > c > i x z > 1 1 1 99 > 2 1 53 99 > 3 2 7 88 > 4 2 3 88 > 5 3 4 77 > 6 3 23 77 > 7 4 6 66 > 8 4 2 66Merge should do the job:> merge(a,b,by=1)i x y 1 1 1 99 2 1 53 99 3 2 7 88 4 2 3 88 5 3 4 77 6 3 23 77 7 4 6 66 8 4 2 66 -- Philippe Glaziou