From: Nicolas Prune <np_at_alambic.org>

Date: Thu 18 Jan 2007 - 13:11:11 GMT

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 Fri Jan 19 00:39:45 2007

Date: Thu 18 Jan 2007 - 13:11:11 GMT

I request your help to optimize a loop.

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

e.g :

my_series <- c(3, 4, 10,14,8,3,4,6,9)

As the last number (9) is higher than the four preceding numbers (6, 4, 3, 8), this function should return 4.

my_series <- c(3, 4, 10,14,8,3,4,11,9)

Here, it should return 0, as 9 is immediately preceeded by a higher number.

So far, I do this awful loop :

result <- 0

for (i in 1:length(my_series-1))

*{
*

if (my_series[length(my_series)-i]>end(my_series)[1])

{ result <- i-1 ; break }

}

I thing there's a better way...

my_series > my_series[end][1] returns :

**TRUE TRUE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
**
, which seems more appealing (once the last "FALSE" is removed), but now, how to
know the size of the last consecutive series of "TRUE" ?

Can you see a better way ?

Thanks.

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 Fri Jan 19 00:39:45 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 Thu 18 Jan 2007 - 14:30:23 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.
*