[R] merging issue.........

Adrian Dusa dusa.adrian at gmail.com
Wed Jan 13 23:25:11 CET 2010


Hi Karean,

If your first object is called obj1 and the second called obj2, then:

> merge(obj1, obj2, all.x=TRUE)
  id trait1 trait2
1  1   10.2    9.8
2  2   11.1   10.8
3  3    9.7     NA
4  6   10.2   10.1
5  7    8.9     NA
6 10    9.7     NA
7 11   10.2     NA

Hope this helps,
Adrian

On Wednesday 13 January 2010, karena wrote:
> hi, I have a question about merging two files.
> For example, I have two files, the first file is like the following:
> 
> id   trait1
> 1    10.2
> 2    11.1
> 3    9.7
> 6    10.2
> 7    8.9
> 10  9.7
> 11  10.2
> 
> The second file is like the following:
> id    trait2
> 1     9.8
> 2     10.8
> 4     7.8
> 5     9.8
> 6     10.1
> 12    10.2
> 13    10.1
> 
> now I want to merge the two files by the variable "id", I only want to keep
> the "id"s which show up in the first file. Even the "id" does not show up
>  in the second file, it doesn't matter, I can keep the missing values. So
>  my question is: how can I merge the two files and keep only the rows whose
>  "id" show up in the first file?
> I know how to do it is SAS, just use the following code:
> merge data1(in=in1) data2(in=in2);
> by id;
> if in1;
> 
> but I really have no idea about how to do it in R.
> 
> thank you in advance,
> 
> karean
> 


-- 
Adrian Dusa
Romanian Social Data Archive
1, Schitu Magureanu Bd.
050025 Bucharest sector 5
Romania
Tel.:+40 21 3126618 \
     +40 21 3120210 / int.101
Fax: +40 21 3158391



More information about the R-help mailing list