Skip to content
Prev 69887 / 398506 Next

cumsum on chron objects

On 5/17/05, Sebastian Luque <sluque at mun.ca> wrote:
I did not understand that you wanted a sequence.

If x and x.locf are as in the previous response then:

   my.seq <- function(x) seq(from = x[1], len = length(x))
   chron(unlist(tapply(x, x.locf, my.seq)))

or if you want to use cumsum:

   xx <- as.vector(x); xx[is.na(xx)] <- 1
   chron(unlist(tapply(xx, x.locf, cumsum)))