[R] Fwd: Merge

arun smartpink111 at yahoo.com
Thu May 23 15:26:57 CEST 2013


You could also do:
library(plyr)
res1<-join(dat1,dat2,type="full")
res1
#   Serialno    name year outcome disch_type
#1         1     ken 1989       d       <NA>
#2         2    mary 1989       a       <NA>
#3         4    john 1989       a       <NA>
#4         5     tom 1989       a       <NA>
#5         6   jolly 1989       d       <NA>
#6        11    mwai 1990    <NA>          d
#7        21 wanjiku 1990    <NA>          a
#8        43   maina 1990    <NA>          a
#9        55    john 1990    <NA>          a
#10       67   welly 1990    <NA>          d


identical(res,res1)
#[1] TRUE
#or
lst1<-list(dat1,dat2)


 Reduce(function(...) merge(...,by=c("Serialno","name","year"),all=TRUE),lst1) 

#  Serialno    name year outcome disch_type
#1         1     ken 1989       d       <NA>
#2         2    mary 1989       a       <NA>
#3         4    john 1989       a       <NA>
#4         5     tom 1989       a       <NA>
#5         6   jolly 1989       d       <NA>
#6        11    mwai 1990    <NA>          d
#7        21 wanjiku 1990    <NA>          a
#8        43   maina 1990    <NA>          a
#9        55    john 1990    <NA>          a
#10       67   welly 1990    <NA>          d
A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Keniajin Wambui <kiangati at gmail.com>
Cc: r-help at r-project.org
Sent: Thursday, May 23, 2013 8:36 AM
Subject: Re: [R] Fwd: Merge

Hello,

Try the following.


rm(list = ls())


dat1 <- read.table(text = "
Serialno  name  year outcome
1             ken    1989   d
2             mary 1989    a
4             john    1989   a
5             tom     1989   a
6             jolly   1989      d
", header = TRUE, stringsAsFactors = FALSE)

dat2 <- read.table(text = "
Serialno  name       year       disch_type
11             mwai    1990       d
21          wanjiku  1990        a
43           maina    1990       a
55            john     1990       a
67             welly   1990       d
", header = TRUE, stringsAsFactors = FALSE)

res <- merge(dat1[, c(1, 4)], dat2[, c(1, 4)], all = TRUE)
res <- merge(merge(res, dat1, all.y = TRUE), merge(res, dat2, all.y = 
TRUE), all = TRUE)
res <- res[, c(1, 4, 5, 2, 3)]
res


Hope this helps,

Rui Barradas

Em 23-05-2013 09:41, Keniajin Wambui escreveu:
> ---------- Forwarded message ----------
> From: Keniajin Wambui <kiangati at gmail.com>
> Date: Thu, May 23, 2013 at 11:36 AM
> Subject: Merge
> To: r-help at r-project.org
>
>
> I am using R 3.01 on R Studio to merge two data sets with approx 120
> variables and the other with 140 variables but with a serialno as the
> unique identifier.
> i.e
>
> Serialno  name  year outcome
> 1             ken    1989   d
> 2             mary 1989    a
> 4             john    1989   a
> 5             tom     1989   a
> 6             jolly   1989      d
>
> and
>
> Serialno  name       year       disch_type
> 11             mwai    1990       d
> 21          wanjiku  1990        a
> 43           maina    1990       a
> 55            john     1990       a
> 67             welly   1990       d
>
> How can I merge them to a common data set without having name.x and
> name.y or year.x and year.y after merging
> --
> Mega Six Solutions
> Web Designer and Research Consultant
> Kennedy Mwai
> 25475211786
>
>
> --
> Mega Six Solutions
> Web Designer and Research Consultant
> Kennedy Mwai
> 25475211786
>
> ______________________________________________
> R-help at r-project.org 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.
>

______________________________________________
R-help at r-project.org 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.




More information about the R-help mailing list