Re: [R] Max consecutive increase in sequence

2008-05-13 Thread Phil Spector
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=st

Re: [R] Max consecutive increase in sequence

2008-05-13 Thread Tony Plate
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$v

Re: [R] Max consecutive increase in sequence

2008-05-13 Thread Marko Milicic
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).

Re: [R] Max consecutive increase in sequence

2008-05-13 Thread Ingmar Visser
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(