I am looking for a function in R that is equivalent to the function "peaks" in Splus. This function gives the local maxima of a vector. I do not find it using the help and I 'm not sure it exits in R. Could someone help me ? Thanks, Herve Cardot -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 28 May 2002, Herve Cardot wrote:> I am looking for a function in R that is equivalent to the function > "peaks" in > Splus. This function gives the local maxima of a vector. > I do not find it using the help and I 'm not sure it exits in R. >It should be something like peaks<-function(x, max=TRUE,na.rm=FALSE){ if (na.rm) omit<-is.na(x) else omit<-FALSE if (max) rval<-1+which(diff(sign(diff(x[!omit])))<0) else rval<-1+which(diff(sign(diff(x[!omit])))>0) if (na.rm){ rval<-rval+cumsum(omit)[rval] } rval } This version doesn't allow the first or last observation to be a peak (and if na.rm=FALSE one before or after a NA can't be a peak either), which is a matter of definition -- I've never used the S-PLUS function so I don't know what it does. Use max=FALSE to get `valleys'. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On 28 May 2002 at 16:26, Herve Cardot wrote:> I am looking for a function in R that is equivalent to the function > "peaks" in Splus. This function gives the local maxima of a vector. I > do not find it using the help and I 'm not sure it exits in R. > > Could someone help me ? > Thanks, >Here is a function originally based on idea of prof.Ripley (slightly modifyed) which shall work like peaks in S+. I did not tested it on R 150 but it worked in former versions correctly. # funkce pro automaticke oznaceni piku ve spektru (peaks) # autor Brian Ripley 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 }> Herve Cardot > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. > -.-.-.-.- 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. > _._._._._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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._