[R] help: selection of data

Martin Ballaschk qcumba at web.de
Sun Oct 26 02:52:04 CEST 2008


Hi Jim.

jim holtman schrieb:
> If you only want the first sequence, 

Yes, that's true, I forgot to say that!

>> x <- c(-10, -5, 0, 5, 10, 15, 20)
>> y <- c(10, 10, 10, -5, -6, -7, 10)
>> data <- as.matrix( cbind(x, y) )
>> # if you only want the first 'run', then use 'rle' to find it
>> mask <- data[,1] > 0 & data[,2] > min(data[,2])
>> run <- rle(mask)
>> # now either the first or second will be TRUE, so find it
>> offset <- cumsum(c(1, run$lengths))  # off set of the start of sequence
>> index <- if (run$values[1]) 1 else 2
>> new_data <- data[seq(from=offset[index], length=run$lengths[index]),]
>>

Although I'm far from understanding that -- it seems to work very well for
me. Will have a look into it tomorrow ...

Thanks!

Cheers
Martin



More information about the R-help mailing list