[R] Doubt in simple merge

PIKAL Petr petr.pikal at precheza.cz
Fri Jan 17 14:01:37 CET 2014


Hi

are you really sure that age in Younger is factor? If it was numeric you can post process result of merge to get rid of NA

Younger$age<-as.numeric(as.character(Younger$age))
Warning message:
NAs introduced by coercion
komplet<-merge(Elder, Younger, all=T)
komplet[complete.cases(komplet),]
   ID age
1 ID1  38
2 ID2  35
3 ID3  31
5 ID4  29
6 ID5  21

If it **is** a factor like in your example

> str(Younger)
'data.frame':   3 obs. of  2 variables:
 $ ID : Factor w/ 3 levels "ID3","ID4","ID5": 2 3 1
 $ age: Factor w/ 3 levels "21","29","NA": 2 1 3

you can do it in similar way but not so easy.
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of kingsly
> Sent: Friday, January 17, 2014 6:14 AM
> To: r-help at r-project.org
> Subject: Re: [R] Doubt in simple merge
>
> Thank you dear friends.  You have cleared my first doubt.
>
> My second doubt:
> I have the same data sets "Elder" and "Younger". 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,"NA"))
>
>
>  Row ID3 comes in both data set. It has a value (31) in "Elder" while
> "NA" in "Younger".
>
> I need output like this.
>
> ID    age
> ID1  38
> ID2  35
> ID3  31
> ID4  29
> ID5  21
>
> Kindly help me.
>
>
>
> On Thursday, 16 January 2014 9:16 PM, Marc Schwartz-3 [via R] <ml-
> node+s789695n4683682h69 at n4.nabble.com> wrote:
>
> 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 <[hidden email]>
> wrote:
>
> ________________________________
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
> ________________________________
>
> If you reply to this email, your message will be added to the
> discussion below: http://r.789695.n4.nabble.com/Doubt-in-simple-merge-
> tp4683671p4683682.html
> To start a new topic under R help, email ml-
> node+s789695n789696h3 at n4.nabble.com
> To unsubscribe from R help, click here.
> NAML
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Doubt-in-
> simple-merge-tp4683671p4683718.html
> Sent from the R help mailing list archive at Nabble.com.
>       [[alternative HTML version deleted]]


________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list