[R] finding peaks in a simple dataset with R

Gabor Grothendieck ggrothendieck at gmail.com
Thu Nov 24 17:33:13 CET 2005


On 11/24/05, Martin Maechler <maechler at stat.math.ethz.ch> wrote:

> NA's have the big drawback of producing a logical vector that
> can NOT be used for subsetting -- and subsetting was exactly a main
> reason for the padding...
>

Since 'which' throws away NAs, one extra 'which' can solve
that.  In the following, runmax from caTools (which is quite
fast being written in C) uses NA padding by default:

x <- c(1:3, 3, 3:5, 5:1) # test data.  peak is at 5.

library(caTools)
x.rv <- rle(x)$values
x.rv[which(runmax(x.rv, 3) == x.rv)]




More information about the R-help mailing list