如何检测R中特定范围内的峰值

如何检测R中特定范围内的峰值,r,time-series,R,Time Series,我有一个时间序列,我想检测(并识别)一些峰值,但只针对R中的特定范围 here is an example ## generate test data with 3 peaks set.seed(123) x <- seq(0, 360, length = 20) y <- abs(rnorm(20, mean = 1, sd = 0.1)) y[5:10] <- c(2, 4, 7, 3, 4, 2) y <- c(y, 0.8 * y, 1.2 * y) x

我有一个时间序列,我想检测(并识别)一些峰值,但只针对R中的特定范围

    here is an example
## generate test data with 3 peaks
set.seed(123)
x <- seq(0, 360, length = 20)
y <- abs(rnorm(20, mean = 1, sd = 0.1))
y[5:10] <- c(2, 4, 7, 3, 4, 2)
y <- c(y, 0.8 * y, 1.2 * y)
x <- seq(0, 360, along = y)
y[6] <- y[7]   # test case with 2 neighbouring equal points
plot(x, y, type="b")
下面是一个例子
##生成具有3个峰值的测试数据
种子集(123)

x类似这样的东西很接近(不确定是否需要两次检测两个值的峰值)

#重现您的数据
种子集(123)

你是说你只想画那些峰?或者你只是想要这些值?我觉得这是重复的,但我现在懒得找到匹配项……看看他们在XCMS包中使用的centwave东西。它对信号进行小波变换以找到峰值,效果非常好。
# Reproduce your data
set.seed(123)
x <- seq(0, 360, length = 20)
y <- abs(rnorm(20, mean = 1, sd = 0.1))
y[5:10] <- c(2, 4, 7, 3, 4, 2)
y <- c(y, 0.8 * y, 1.2 * y)
x <- seq(0, 360, along = y)
y[6] <- y[7]   # test case with 2 neighbouring equal points
plot(x, y, type="b")

# shift y up and down a position (for peak identification)
yu <- c(tail(y, -1), NA)
yd <- c(NA, head(y, -1))

# identify peaks that are in the correct range 
# where y is higher than the point before and after
high <- which(y - yu >= 0 & y - yd >= 0 & y > 6 & y < 9)
low  <- which(y - yu >= 0 & y - yd >= 0 & y >= 2 & y <= 4) # one peak is at 4

# plot lines at peaks
abline(v = x[high], col = 'blue')
abline(v = x[low], col = 'red')