[R] Doubt in simple merge

Marc Schwartz marc_schwartz at me.com
Thu Jan 16 16:31:25 CET 2014


Not quite:

> rbind(Elder, Younger)
   ID age
1 ID1  38
2 ID2  35
3 ID3  31
4 ID4  29
5 ID5  21
6 ID3  31

Note that ID3 is duplicated.


Should be:

> merge(Elder, Younger, by = c("ID", "age"), all = TRUE)
   ID age
1 ID1  38
2 ID2  35
3 ID3  31
4 ID4  29
5 ID5  21


He wants to do a join on both "ID" and "age" to avoid duplications of rows when the same ID and age occur in both data frames. If the same column names (eg "Var") appears in both data frames and are not part of the 'by' argument, you end up with Var.x and Var.y in the result.

In the case of two occurrences of the same ID but two different ages, if that is possible, both rows would be added to the result using the above code.

Regards,

Marc Schwartz


On Jan 16, 2014, at 9:04 AM, Frede Aakmann Tøgersen <frtog at vestas.com> wrote:

> Ups, sorry that should have been
> 
> mer <- rbind(Elder, Younger)
> 
> /frede
> 
> 
> -------- Oprindelig meddelelse --------
> Fra: Frede Aakmann Tøgersen
> Dato:16/01/2014 15.54 (GMT+01:00)
> Til: "Adams, Jean" ,kingsly
> Cc: R help
> Emne: Re: [R] Doubt in simple merge
> 
> No I think the OP wants
> 
> mer <- merge(Elder, Younger)
> 
> Br. Frede
> 
> 
> -------- Oprindelig meddelelse --------
> Fra: "Adams, Jean"
> Dato:16/01/2014 15.45 (GMT+01:00)
> Til: kingsly
> Cc: R help
> Emne: Re: [R] Doubt in simple merge
> 
> You are telling it to merge by ID only.  But it sounds like you would like
> it to merge by both ID and age.
> 
> merge(Elder, Younger, all=TRUE)
> 
> Jean
> 
> 
> On Thu, Jan 16, 2014 at 6:25 AM, kingsly <ecokingsly at yahoo.co.in> wrote:
> 
>> Dear R community
>> 
>> I have a two data set called "Elder" and "Younger".
>> This is my code for simple merge.
>> 
>> Elder <- data.frame(
>>  ID=c("ID1","ID2","ID3"),
>>  age=c(38,35,31))
>> Younger <- data.frame(
>>  ID=c("ID4","ID5","ID3"),
>>  age=c(29,21,31))
>> 
>> mer <- merge(Elder,Younger,by="ID", all=T)
>> 
>> Output I am expecting:
>> 
>> ID    age
>> ID1  38
>> ID2  35
>> ID3  31
>> ID4  29
>> ID5  21
>> 
>> It looks very simple.  But I need help.
>> When I run the code it gives me age.x and age.y.
>> thank you




More information about the R-help mailing list