[R] how to merge two data.frames – only complete observations

Marc Schwartz marc_schwartz at me.com
Tue Nov 17 19:36:31 CET 2009


On Nov 17, 2009, at 12:20 PM, Martin Batholdy wrote:

> Hi,
>
>
> I have two data-frames like:
>
> A:
> USA	3.2
> Canada	4.7
> Austria	1.5
> Iran		0.3
> China	3.8
> Japan	3.0
>
> B:
> Austria	17
> Iran		22
> Angola	29
> Japan	32
> England	11
>
>
> Now I want to merge this two data.frames to one -
> but I only want entries for the countries I have complete  
> observations for;
>
> so the final data.frame should look like this:
>
> C:
> Austria	1.5	17
> Iran		0.3	22
> Japan	3.0	32	
>
>
> How can I do this?


See ?merge

 > A
        V1  V2
1     USA 3.2
2  Canada 4.7
3 Austria 1.5
4    Iran 0.3
5   China 3.8
6   Japan 3.0


 > B
        V1 V2
1 Austria 17
2    Iran 22
3  Angola 29
4   Japan 32
5 England 11


 > merge(A, B, by = "V1")
        V1 V2.x V2.y
1 Austria  1.5   17
2    Iran  0.3   22
3   Japan  3.0   32


HTH,

Marc Schwartz




More information about the R-help mailing list