Yes Gabor, you definition (a sequence of numbers which each increase by 1
over the prior
number) is what I meant. Sorry it that was not clear and I thank you and
Joshua for your time and your explanation.
This should work fine.
Gabor Grothendieck wrote:
>
> On Wed, Jul 6, 2011 at 7:32 PM, B
On Wed, Jul 6, 2011 at 7:32 PM, B77S wrote:
> Hi,
>
> How can I discern which elements in x (see below) are in 'order', but more
> specifically.. only the 1st 'ordered run'?
> I would like for it to return elements 1:8... there may be ordered values
> after 1:8, but those are not of interest.
>
>
I seem to recall seeing this done in one or two elegant lines, but
run <- function(x, type = 1) {
index <- rle(diff(c(NA, x)))
i <- cumsum(index$lengths)
j <- match(type, index$values)
x[seq.int(i[j - 1], i[j])]
}
run(c(1, 2, 3, 4, 5, 6, 7, 8, 20, 21, 22, 45))
run(c(20, 22, 24, 26, 1,
well.. the following works, but if you have another idea I am still
interested.
1:(which(diff(x)!=1)[1])
B77S wrote:
>
> Hi,
>
> How can I discern which elements in x (see below) are in 'order', but more
> specifically.. only the 1st 'ordered run'?
> I would like for it to return eleme
Hi,
If an "ordered run" means the difference is between the ith and ith +
1 position is 1, then:
out <- rle(diff(x))
?diff gives you the differences (i + 1) - (i), and then run length
encoding encodes how long a run of the same number is. In this case,
there first run is length 7.
rle() output
Hi,
How can I discern which elements in x (see below) are in 'order', but more
specifically.. only the 1st 'ordered run'?
I would like for it to return elements 1:8... there may be ordered values
after 1:8, but those are not of interest.
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 20, 21, 22, 45)
Thanks fo
6 matches
Mail list logo