[R] Match data.frames with different number of rows

Philippe Glaziou glaziou at pasteur-kh.org
Wed Nov 5 12:47:27 CET 2003


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	66


Merge 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




More information about the R-help mailing list