[R] coalesce in its

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Mon Apr 4 20:27:02 CEST 2005


On Mon, 4 Apr 2005 13:46:46 -0400 Omar Lakkis wrote:

> I have two data sets that I converted to its objects to get:
> 
> > ts1 
>         date  settle
> 1 2000-09-29 107.830
> 2 2000-10-02 108.210
> 3 2000-10-03 108.800
> 4 2000-10-04 108.800
> 5 2000-10-05 109.155
> ts2>
>         date  settle
> 1 2000-09-25 107.610
> 2 2000-09-26 107.585
> 3 2000-09-27 107.385
> 4 2000-09-28 107.595
> 5 2000-09-29 107.875
> 6 2000-10-02 108.805
> 7 2000-10-03 108.665
> 8 2000-10-04 109.280
> 9 2000-10-05 109.290
> 
> I want to get a list of the values of ts1 with the missing dates
> substitute from ts2. When I do union(ts1,ts2) I get
> 
> > u
>                  1       1
> 2000-09-24      NA 107.610
> 2000-09-25      NA 107.585
> 2000-09-26      NA 107.385
> 2000-09-27      NA 107.595
> 2000-09-28 107.830 107.875
> 2000-10-01 108.210 108.805
> 2000-10-02 108.800 108.665
> 2000-10-03 108.800 109.280
> 2000-10-04 109.155 109.290
> 
> Other than looping, is there a way to get the first column with NA
> values substituted from the second column?

If I understand you correctly, you want:

R> ts3 <- union(ts1, ts2)
R> repIndex <- is.na(ts3[,1])
R> ts3[repIndex, 1] <- ts3[repIndex, 2]
R> ts3[,1]
            settle
2000-09-25 107.610
2000-09-26 107.585
2000-09-27 107.385
2000-09-28 107.595
2000-09-29 107.830
2000-10-02 108.210
2000-10-03 108.800
2000-10-04 108.800
2000-10-05 109.155

hth,
Z

> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list