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