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

2003-11-05 Thread Bernd Weiss
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

 a
  i  x
 1  1
 1 53
 2  7
 2  3
 3  4
 3 23
 4  6
 4  2

and b like this

 b
  j  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

 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

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

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help


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

2003-11-05 Thread Philippe Glaziou
Bernd Weiss [EMAIL PROTECTED] 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  xz
 1 1  199
 2 1 5399
 3 2  788  
 4 2  388
 5 3  477  
 6 3 2377
 7 4  666
 8 4  266


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

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help