# [R] peaks

Petr Pikal petr.pikal at precheza.cz
Wed Oct 16 12:14:05 CEST 2002

```
On 15 Oct 2002 at 18:09, Rieckermann Joerg wrote:

> Dear Petr,
>
> I have been fooling around with this peaks function of yours/Ripley
> and don't seem to get the main idea.
>
> What I intend to do is locate the peaks in a vector and later use thes
> values. So the position of the peaks would be of great interest.
>
> My questions:
> * Why does peak retunr a smaller vector?
it gives you a shorter vector due to span value. The greater the span value the
shorter resulting vector.

> * How can I correct for it to correctly locate the peaks?

use odd numbers for span - 3,5,7....

x<-c(1:44,103,46:75,103,77:100)
> x.t<-peaks(x,21)
> seq(along=x)[x.t]
[1] 45 76
> x[x.t]
[1] 103 103

> x.t<-peaks(x,20)
> seq(along=x)[x.t]
[1] 46 77
> x[x.t]
[1] 46 77

if you use bigger span numbers you can get rid of some peaks which are near one
to another eg select only "important" peaks. However you can miss some peaks,
especially near the ends of your vector.

>
> > x<-c(1:44,103,46:75,103,77:100)
> > peaks<-function(series,span=3)
> + {
> + z <- embed(series, span)
> + s <- span%/%2
> + v<- max.col(z) == 1 + s
> + result <- c(rep(FALSE,s),v)
> + result <- result[1:(length(result)-s)]
> + result
> + }
> > t<-peaks(x,20)
> > t
>  [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> FALSE FALSE [14] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> FALSE FALSE FALSE FALSE [27] FALSE FALSE FALSE FALSE FALSE FALSE FALSE
> FALSE FALSE FALSE FALSE FALSE FALSE [40] FALSE FALSE FALSE FALSE FALSE
> FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE [53] FALSE FALSE FALSE
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE [66] FALSE
> FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
> [79] FALSE FALSE FALSE
>
> > length(t)
> [1] 81
> > length(x)
> [1] 100
> > which(x>100)	#peak position in x
> [1] 45 76
> > which(t==T)		#peak position i get from 'peaks'
> [1] 46 77
>
> I would be grateful for any hint.
>
> Best regards,
> Joerg
>
>
>
Petr Pikal
petr.pikal at precheza.cz
p.pik at volny.cz

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```