[R] How to optimize this loop ?

Kaskelma, Heikki kashei at sip-oy.com
Tue Jan 23 19:18:36 CET 2007


>Given a series of observations, I want to know how many consecutive
past
>observations are below the last one.

prune=function(m)
{ mr=rev(m)
  ms=cumsum(mr < mr[1])
  sum(seq_along(ms) - ms == 1) - 1
}

prune(c(3, 4, 10, 14, 8, 3, 4, 8, 9))  # 4
prune(c(3, 4, 10, 14, 8, 3, 4, 11, 9)) # 0


Heikki Kaskelma



More information about the R-help mailing list