[R] merge two dataset and replace missing by 0

David Winsemius dwinsemius at comcast.net
Mon Nov 15 17:06:24 CET 2010


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



More information about the R-help mailing list