Hi r users, I have two data sets (X1, X2). For example, time1<-c( 0, 8, 15, 22, 43, 64, 85, 106, 127, 148, 169, 190 ,211 ) outpue1<-c(171 ,164 ,150 ,141 ,109 , 73 , 47 ,26 ,15 ,12 ,6 ,2 ,1 ) X1<-cbind(time1,outpue1) time2<-c( 0 ,8 ,15 , 22 ,43 , 64 ,85 ,106 ,148) output2<-c( 5 ,5 ,4 ,5 ,5 ,4 ,1 ,2 , 1 ) X2<-cbind(time2,output2) I want to merge X1 and X2 into a big dataset X by time1 and time2 so that the missing item in output2 will be replace by 0. For example, there is no output2 when time2=127, then the corresponding output will be 0. Anyone know how to use merge command to deal with this? Thanks, Kate [[alternative HTML version deleted]]
See ?merge with argument all=TRUE and replace by 0 afterwards. Uwe Ligges On 15.11.2010 16:42, Kate Hsu wrote:> Hi r users, > > I have two data sets (X1, X2). For example, > time1<-c( 0, 8, 15, 22, 43, 64, 85, 106, 127, 148, 169, 190 ,211 ) > outpue1<-c(171 ,164 ,150 ,141 ,109 , 73 , 47 ,26 ,15 ,12 ,6 ,2 ,1 ) > X1<-cbind(time1,outpue1) > > time2<-c( 0 ,8 ,15 , 22 ,43 , 64 ,85 ,106 ,148) > output2<-c( 5 ,5 ,4 ,5 ,5 ,4 ,1 ,2 , 1 ) > X2<-cbind(time2,output2) > > I want to merge X1 and X2 into a big dataset X by time1 and time2 so that > the missing item in output2 will be replace by 0. For example, there is no > output2 when time2=127, then the corresponding output will be 0. Anyone know > how to use merge command to deal with this? > > Thanks, > > Kate > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
On Nov 15, 2010, at 10:42 AM, Kate Hsu wrote:> Hi r users, > > I have two data sets (X1, X2). For example, > time1<-c( 0, 8, 15, 22, 43, 64, 85, 106, 127, 148, 169, 190 , > 211 ) > outpue1<-c(171 ,164 ,150 ,141 ,109 , 73 , 47 ,26 ,15 ,12 ,6 , > 2 ,1 ) > X1<-cbind(time1,outpue1) > > time2<-c( 0 ,8 ,15 , 22 ,43 , 64 ,85 ,106 ,148) > output2<-c( 5 ,5 ,4 ,5 ,5 ,4 ,1 ,2 , 1 ) > X2<-cbind(time2,output2) > > I want to merge X1 and X2 into a big dataset X by time1 and time2 so > that > the missing item in output2 will be replace by 0. For example, there > is no > output2 when time2=127, then the corresponding output will be 0. > Anyone know > how to use merge command to deal with this?> merge(X1,X2, by.x="time1", by.y="time2", all=TRUE) time1 outpue1 output2 1 0 171 5 2 8 164 5 3 15 150 4 4 22 141 5 5 43 109 5 6 64 73 4 7 85 47 1 8 106 26 2 9 127 15 NA 10 148 12 1 11 169 6 NA 12 190 2 NA 13 211 1 NA> > Thanks, > > Kate > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
On Mon, Nov 15, 2010 at 10:42 AM, Kate Hsu <yhsu.rhelp at gmail.com> wrote:> Hi r users, > > I have two data sets (X1, X2). For example, > time1<-c( 0, ? 8, ?15, ?22, ?43, ?64, ?85, 106, 127, 148, 169, 190 ,211 ) > outpue1<-c(171 ,164 ,150 ,141 ,109 , 73 , 47 ?,26 ?,15 ?,12 ? ,6 ? ,2 ? ,1 ) > X1<-cbind(time1,outpue1) > > time2<-c( 0 ? ,8 ?,15 , 22 ?,43 , 64 ?,85 ,106 ,148) > output2<-c( 5 ? ,5 ? ,4 ? ,5 ? ,5 ? ,4 ? ,1 ? ,2 , ?1 ) > X2<-cbind(time2,output2) > > I want to merge X1 and X2 into a big dataset X by time1 and time2 so that > the missing item in output2 will be replace by 0. For example, there is no > output2 when time2=127, then the corresponding output will be 0. Anyone know > how to use merge command to deal with this? >Since these are time series you might want to use a time series package to do this: library(zoo) time1<-c( 0, 8, 15, 22, 43, 64, 85, 106, 127, 148, 169, 190 ,211 ) output1<-c(171 ,164 ,150 ,141 ,109 , 73 , 47 ,26 ,15 ,12 ,6 ,2 ,1 ) time2<-c( 0 ,8 ,15 , 22 ,43 , 64 ,85 ,106 ,148) output2<-c( 5 ,5 ,4 ,5 ,5 ,4 ,1 ,2 , 1 ) z1 <- zoo(output1, time1) z2 <- zoo(output2, time2) merge(z1, z2, fill = 0) -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com