如何在以下示例中找到红色vlin指示的索引:
# Get the data as "tmpData"
source("http://pastie.org/pastes/9350691/download")
# Plot
plot(tmpData,type="l")
abline(v=49,col="red")
以下方法很有前景,但如何找到峰值最大值?
library(RcppRoll)
n <- 10
smoothedTmpData <- roll_mean(tmpData,n)
plot(-diff(smoothedTmpData),type="l")
abline(v=49,col="red")
答案 0 :(得分:2)
which.max(-diff(smoothedTmpData))
为您提供最大值的索引。
http://www.inside-r.org/r-doc/base/which.max
我不确定这是你的实际问题......
答案 1 :(得分:1)
如果渐变中存在单个峰,如示例数据集中那么gwieshammer是正确的:您可以使用which.max
来查找它。
对于存在多个可能峰值的情况,您需要更复杂的方法。 R具有许多峰值发现功能(质量不同)。适用于此数据的一个是wavCWTPeaks
中的wmtsa
。
library(RcppRoll)
library(wmtsa)
source("http://pastie.org/pastes/9350691/download")
n <- 10
smoothedTmpData <- roll_mean(tmpData, n)
gradient <- -diff(smoothedTmpData)
cwt <- wavCWT(gradient)
tree <- wavCWTTree(cwt)
(peaks <- wavCWTPeaks(tree))
## $x
## [1] 4 52
##
## $y
## [1] 302.6718 5844.3172
##
## attr(,"peaks")
## branch itime iscale time scale extrema iendtime
## 1 1 5 2 5 2 16620.58 4
## 2 2 57 26 57 30 20064.64 52
## attr(,"snr.min")
## [1] 3
## attr(,"scale.range")
## [1] 1 28
## attr(,"length.min")
## [1] 10
## attr(,"noise.span")
## [1] 5
## attr(,"noise.fun")
## [1] "quantile"
## attr(,"noise.min")
## 5%
## 4.121621
因此可以正确找到接近50的主峰,并且例程在开始时拾取另一个较小的峰值。