Skip to content

how to add a column from another dataset with "merge"

2 messages · Pete Brecknock, arun

#
kiotoqq wrote
how about ...

#Data
d1<-data.frame(id=c(9,8,6,4,4,3,1))
d2<-data.frame(id=c(9,8,6),age=c(46,56,52))

# Left Merge
d<-merge(d1,d2,all.x=TRUE)

# Reorder
d[order(d$id,decreasing=TRUE),]

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-column-from-another-dataset-with-merge-tp4652482p4652486.html
Sent from the R help mailing list archive at Nabble.com.
#
Hi,
You could use sort=FALSE in ?merge()
d1<-data.frame(id=c(9,8,6,5,4,3,1))
d2<-data.frame(id=c(9,8,6),age=c(46,56,52))
?d<-merge(d1,d2,all.x=TRUE,sort=FALSE)
#or
library(plyr)
join(d1,d2,by="id",type="full")
#? id age
#1? 9? 46
#2? 8? 56
#3? 6? 52
#4? 5? NA
#5? 4? NA
#6? 3? NA
#7? 1? NA
A.K.

----- Original Message -----
From: Pete Brecknock <Peter.Brecknock at bp.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, December 7, 2012 9:12 AM
Subject: Re: [R] how to add a column from another dataset with "merge"

kiotoqq wrote
how about ...

#Data
d1<-data.frame(id=c(9,8,6,4,4,3,1))
d2<-data.frame(id=c(9,8,6),age=c(46,56,52))

# Left Merge
d<-merge(d1,d2,all.x=TRUE)

# Reorder
d[order(d$id,decreasing=TRUE),]

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/how-to-add-a-column-from-another-dataset-with-merge-tp4652482p4652486.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.