[R] local maxima positions in a vector with duplicated values

Greg Minshall m|n@h@|| @end|ng |rom um|ch@edu
Fri Mar 26 19:27:40 CET 2021


Stefano,

my contribution is similar to Bill Dunlap's:

  x <- c(1,0,0,0,2,2,3,4,0,1,1,0,5,5,5,0,1)
  (cumsum(rle(x)$lengths)-(rle(x)$length-1))[which(diff(diff(rle(x)$values)>=0)<0)+1]
  cumsum(rle(x)$lengths)[which(diff(diff(rle(x)$value)>0)>0)+1]

kind of a cute little problem, actually.

cheers, Greg



More information about the R-help mailing list