[R] Fwd: Merge

2 views
Skip to first unread message

Keniajin Wambui

unread,
May 23, 2013, 4:41:39 AM5/23/13
to r-h...@r-project.org
---------- Forwarded message ----------
From: Keniajin Wambui <kian...@gmail.com>
Date: Thu, May 23, 2013 at 11:36 AM
Subject: Merge
To: r-h...@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-h...@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.

Rui Barradas

unread,
May 23, 2013, 8:36:25 AM5/23/13
to Keniajin Wambui, r-h...@r-project.org
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

arun

unread,
May 23, 2013, 9:26:57 AM5/23/13
to Rui Barradas, R help
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.
Reply all
Reply to author
Forward
0 new messages