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
Dear R Users,
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
and vectorise, vectorise, vectorise.
HTH
Petr
On 18 Jan 2007 at 14:11, Nicolas Prune wrote:
Date sent: Thu, 18 Jan 2007 14:11:11 +0100
From: Nicolas Prune [EMAIL PROTECTED]
To: r-help@stat.math.ethz.ch
Subject:[R] How to optimize
Nicolas Prune wrote:
Dear R Users,
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
PROTECTED]
To: r-help@stat.math.ethz.ch
Subject: [R] How to optimize this loop ?
Dear R Users,
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
Sorry, I accidentaly lost one line of the function code (result -0),
see below...
Regards, Martin
Martin Becker schrieb:
myfun1 - function(series=c(3, 4, 10,14,8,3,4,6,9)) {
result - 0# NEW
len-length(series)
for (i in (len-1):1)
{
if
Thanks to everyone who answered, in public or in private.
I successfully rewrote my function with rle.
For my sample, the total computation times (I call this function around 1000
times) are roughly the same. However, I thank you for letting me know rle, and
S Poetry.
Regards,
Nicolas
G'day Martin and others,
On Thu, 18 Jan 2007 15:22:07 +0100
Martin Becker [EMAIL PROTECTED] wrote:
Sorry, I accidentaly lost one line of the function code (result -0),
see below...
But even with that line, the code doesn't work properly. :)
Since you changed the order of the for loop (from i