Max consecutive increase in sequence

View: New views
5 Messages — Rating Filter:   Alert me  

Max consecutive increase in sequence

by Marko Milicic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all R helpers,

I'm trying to comeup with nice and elegant way of "detecting" consecutive
increases/decreases in the sequence of numbers. I'm trying with combination
of which() and diff() functions but unsuccesifuly.

For example:

sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1);

I'd like to find way to calculate

a) maximum consecutive increase = 3 (from 1 to 4)
b) maximum consecutive decrease = 5 (from 6 to 1)

All ideas are highly welcomed!





--
This e-mail and any files transmitted with it are confid...{{dropped:14}}

______________________________________________
R-help@... 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.

Re: Max consecutive increase in sequence

by Ingmar Visser :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

rle(diff(sq)) could be helpful here,
best, Ingmar

On May 13, 2008, at 11:19 PM, Marko Milicic wrote:

> Hi all R helpers,
>
> I'm trying to comeup with nice and elegant way of "detecting"  
> consecutive
> increases/decreases in the sequence of numbers. I'm trying with  
> combination
> of which() and diff() functions but unsuccesifuly.
>
> For example:
>
> sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1);
>
> I'd like to find way to calculate
>
> a) maximum consecutive increase = 3 (from 1 to 4)
> b) maximum consecutive decrease = 5 (from 6 to 1)
>
> All ideas are highly welcomed!
>
>
>
>
>
> --
> This e-mail and any files transmitted with it are confid...
> {{dropped:14}}
>
> ______________________________________________
> R-help@... 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.

______________________________________________
R-help@... 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.

Parent Message unknown Re: Max consecutive increase in sequence

by Marko Milicic :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Erik,

If you look at first 4 numbers, you will se that there was one increase
between first and second number (1 and 2), immediatly after that increase,
there is an increase between second and third number (2 and 3) and finaly
third consecutive increse between third and fourth number (3 and 4).

If you follow similar logic, you will identify that from 7th to 8th number
sequence decreased (from 6 to 5)...followed by 4 consecutive decreases
(until 12th and 13th number - from 2 to 1)...

Thanks for your time





On Tue, May 13, 2008 at 10:25 PM, Erik Iverson <iverson@...>
wrote:

> Are you sure you gave us the right 'sq'?  I don't understand what you want
> if so.
>
>
> How does 1 to 4 come from sq ?
>
>
> Marko Milicic wrote:
>
> > Hi all R helpers,
> >
> > I'm trying to comeup with nice and elegant way of "detecting"
> > consecutive
> > increases/decreases in the sequence of numbers. I'm trying with
> > combination
> > of which() and diff() functions but unsuccesifuly.
> >
> > For example:
> >
> > sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1);
> >
> > I'd like to find way to calculate
> >
> > a) maximum consecutive increase = 3 (from 1 to 4)
> > b) maximum consecutive decrease = 5 (from 6 to 1)
> >
> > All ideas are highly welcomed!
> >
> >
> >
> >
> >
> >


--
This e-mail and any files transmitted with it are confid...{{dropped:14}}

______________________________________________
R-help@... 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.

Re: Max consecutive increase in sequence

by Tony Plate :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

If the increases or decreases could be any size,
rle(sign(diff(x))) could do it:

 > x <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1)
 > r <- rle(sign(diff(x)))
 > r
Run Length Encoding
   lengths: int [1:5] 3 2 2 5 4
   values : num [1:5] 1 0 1 -1 0
 > i1 <- which(r$lengths==max(r$lengths[r$values==1]) &
r$values==1)[1]
 > i2 <- which(r$lengths==max(r$lengths[r$values==-1]) &
r$values==-1)[1]
 > i1
[1] 1
 > i2
[1] 4
 > rbind(up=c(start=cumsum(c(1, r$lengths))[i1],
len=r$lengths[i1]), down=c(start=cumsum(c(1,
r$lengths))[i2], len=r$lengths[i2]))
      start len
up       1   3
down     8   5
 >

Ingmar Visser wrote:

> rle(diff(sq)) could be helpful here,
> best, Ingmar
>
> On May 13, 2008, at 11:19 PM, Marko Milicic wrote:
>
>> Hi all R helpers,
>>
>> I'm trying to comeup with nice and elegant way of "detecting" consecutive
>> increases/decreases in the sequence of numbers. I'm trying with
>> combination
>> of which() and diff() functions but unsuccesifuly.
>>
>> For example:
>>
>> sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1);
>>
>> I'd like to find way to calculate
>>
>> a) maximum consecutive increase = 3 (from 1 to 4)
>> b) maximum consecutive decrease = 5 (from 6 to 1)
>>
>> All ideas are highly welcomed!
>>
>>
>>
>>
>>
>> --
>> This e-mail and any files transmitted with it are confid...{{dropped:14}}
>>
>> ______________________________________________
>> R-help@... 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.
>
> ______________________________________________
> R-help@... 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.
>

______________________________________________
R-help@... 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.

Re: Max consecutive increase in sequence

by Phil Spector :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I believe the original poster was looking for runs of consecutive
values.  Here's a generalization of Tony's solution:

findlong = function(seq){
     rr = rle(seq)
     lens = rr$length
     lens[rr$value == FALSE] = 0
     ll = which.max(lens)
     start = cumsum(c(1,rr$length))[ll]
     list(start=start,length=rr$lengths[ll])
}

> sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1)

Then

> findlong(diff(sq) == 1)       # starts at position 1, run of 3
$start
[1] 1

$length
[1] 3

> findlong(diff(sq) == -1)      # starts at position 8, run of 5
$start
[1] 8

$length
[1] 5

                                        - Phil Spector
  Statistical Computing Facility
  Department of Statistics
  UC Berkeley
  spector@...



On Tue, 13 May 2008, Tony Plate wrote:

> If the increases or decreases could be any size, rle(sign(diff(x))) could do
> it:
>
>> x <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1)
>> r <- rle(sign(diff(x)))
>> r
> Run Length Encoding
>  lengths: int [1:5] 3 2 2 5 4
>  values : num [1:5] 1 0 1 -1 0
>> i1 <- which(r$lengths==max(r$lengths[r$values==1]) & r$values==1)[1]
>> i2 <- which(r$lengths==max(r$lengths[r$values==-1]) & r$values==-1)[1]
>> i1
> [1] 1
>> i2
> [1] 4
>> rbind(up=c(start=cumsum(c(1, r$lengths))[i1], len=r$lengths[i1]),
> down=c(start=cumsum(c(1, r$lengths))[i2], len=r$lengths[i2]))
>     start len
> up       1   3
> down     8   5
>>
>
> Ingmar Visser wrote:
>> rle(diff(sq)) could be helpful here,
>> best, Ingmar
>>
>> On May 13, 2008, at 11:19 PM, Marko Milicic wrote:
>>
>>> Hi all R helpers,
>>>
>>> I'm trying to comeup with nice and elegant way of "detecting" consecutive
>>> increases/decreases in the sequence of numbers. I'm trying with
>>> combination
>>> of which() and diff() functions but unsuccesifuly.
>>>
>>> For example:
>>>
>>> sq <- c(1, 2, 3, 4, 4, 4, 5, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1);
>>>
>>> I'd like to find way to calculate
>>>
>>> a) maximum consecutive increase = 3 (from 1 to 4)
>>> b) maximum consecutive decrease = 5 (from 6 to 1)
>>>
>>> All ideas are highly welcomed!
>>>
>>>
>>>
>>>
>>>
>>> --
>>> This e-mail and any files transmitted with it are confid...{{dropped:14}}
>>>
>>> ______________________________________________
>>> R-help@... 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.
>>
>> ______________________________________________
>> R-help@... 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.
>>
>
> ______________________________________________
> R-help@... 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.
>

______________________________________________
R-help@... 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.