[R] merging 2 frames while keeping all the entries from the "reference" frame

From: Dimitri Liakhovitski <dimitri.liakhovitski_at_gmail.com>
Date: Mon, 04 Apr 2011 13:09:02 -0400


Hello!
I have my data frame "mydata" (below) and data frame "reference" - that contains all the dates I would like to be present in the final data frame.
I am trying to merge them so that the the result data frame contains all 8 dates in both subgroups (i.e., Group1 should have 8 rows and Group2 too). But when I merge it it's not coming out this way. Any hint would be greatly appreciated!
Dimitri

mydata<-data.frame(mydate=rep(seq(as.Date("2008-12-29"), length = 8, by = "week"),2),
group=c(rep("Group1",8),rep("Group2",8)),values=rnorm(16,1,1))
(reference);(mydata)

set.seed(1234)
out<-sample(1:16,5,replace=F)
mydata<-mydata[-out,]; dim(mydata)
(mydata)

# "reference" contains the dates I want to be present in the final data frame: reference<-data.frame(mydate=seq(as.Date("2008-12-29"), length = 8, by = "week"))

# Merging:
new.data<-merge(mydata,reference,by="mydate",all.x=T,all.y=T) new.data<-new.data[order(new.data$group,new.data$mydate),]
(new.data)

# my new.data contains only 7 rows in Group 1 and 4 rows in Group 2

-- 
Dimitri Liakhovitski
Ninah Consulting

______________________________________________
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.
Received on Mon 04 Apr 2011 - 17:12:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 19:20:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive