Re: [R] How to optimize this loop ?

From: Kaskelma, Heikki <kashei_at_sip-oy.com>
Date: Tue 23 Jan 2007 - 18:18:36 GMT


>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



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Wed Jan 24 05:29:31 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 23 Jan 2007 - 18:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.