[R] Peak detector help!?
petr.pikal at precheza.cz
Fri Feb 8 10:22:15 CET 2013
I am not sure if it suits your needs but I use this function for identifying peaks in different spectral data.
peaks <- function (series, span = 3, ties.method = "first")
if ((span <- as.integer(span))%%2 != 1)
stop("'span' must be odd")
z <- embed(series, span)
s <- span%/%2
v <- max.col(z, ties.method = ties.method) == 1 + s
pad <- rep(FALSE, s)
result <- c(pad, v, pad)
Works for both your examples.
test <- c(9,8,7,5,4,1,1,2,1,1,3,4,5,6,7,5,4,3,2,1,1,3,4,5,6,7,8)
 FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE
 FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 FALSE FALSE FALSE
test <- c(rep(1,10),20,rep(1,10))
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Johannes Graumann
> Sent: Thursday, February 07, 2013 1:42 PM
> To: r-help at stat.math.ethz.ch
> Subject: Re: [R] Peak detector help!?
> Johannes Graumann wrote:
> > Grrr ... new trial with code here: http://pastebin.com/RjHNNG9J Maybe
> > the amount of inline-code prevented posting?
> > Hello,
> > I am writing a simple peak detector and it works quite well ...
> > however there's one special case below, that I can't get my head
> > wrapped around ... the problem is in the "Deal with not fully
> > qualified peaks at the sequence extremes" section, but I cannot seem
> > to come up with a condition that would be met in the special case
> > below and base a fix on it ... mind completely poisoned with trial
> solutions that didn't work ...
> > A fresh hint anyone?
> > Sincerely, Joh
> Sleep brought some insight.
> The code here http://pastebin.com/UXzbzqp8 works for now - I have
> already quite a number of test cases that gave me problems and now
> don't ...
> probably more corner cases somewhere, but they will be dealt with as
> they show up.
> Cheers, Joh
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help