Fill values in a zoo object
On Sun, Sep 5, 2010 at 6:46 PM, Wilfred Daye <wdaye01 at gmail.com> wrote:
Hi, I have a tmp object which is stream of numbers of -1, 0, 1, 2. ?I want
to make values from 2007-09-05 to 2007-09-13 to be 0, and from 2007-09-14 to
2007-09-27 to be 1, and 2007-09-28 to 2007-10-03 to be 0 again. ?To logic is
that suppose t(i)=1, then t(i+n)=1 if t(i+n) already is {0 or 1}, but not
2. ?However, if i(i) is already {0 or 2}, then t(i+n) = 0, where n represent
the next n days. ?What is the best approach without loops? ?Thank you.
class(tmp)
[1] "xts" "zoo"
tmp[40:60]
? ? ? ? ? Tmp 2007-09-05 ? ? ? ? ? ? 2 2007-09-06 ? ? ? ? ? ? 0 2007-09-07 ? ? ? ? ? ? 2 2007-09-10 ? ? ? ? ? ? 0 2007-09-11 ? ? ? ? ? ? 0 2007-09-12 ? ? ? ? ? ? 0 2007-09-13 ? ? ? ? ? ? 0 2007-09-14 ? ? ? ? ? ? 1 2007-09-17 ? ? ? ? ? ? 0 2007-09-18 ? ? ? ? ? ? 0 2007-09-19 ? ? ? ? ? ? 0 2007-09-20 ? ? ? ? ? ? 0 2007-09-21 ? ? ? ? ? ? 0 2007-09-24 ? ? ? ? ? ? 1 2007-09-25 ? ? ? ? ? ? 0 2007-09-26 ? ? ? ? ? ? 0 2007-09-27 ? ? ? ? ? ? 0 2007-09-28 ? ? ? ? ? ? 2 2007-10-01 ? ? ? ? ? ? 0 2007-10-02 ? ? ? ? ? ? 0 2007-10-03 ? ? ? ? ? ? 2
Try na.locf: library(zoo) z <- structure(c(0, NA, 0, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, 0, NA, NA, 0), index = structure(c(13761, 13762, 13763, 13766, 13767, 13768, 13769, 13770, 13773, 13774, 13775, 13776, 13777, 13780, 13781, 13782, 13783, 13784, 13787, 13788, 13789), class = "Date"), class = "zoo") z[z == 0] <- NA z[z == 2] <- 0 na.locf(z)
Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com