Hoew can I apply vector of observations to find the local maximum between each observation in R. I do the following code, but according to plot the result should be just two local maximum. How can I do this in R?
x = c(0.0000005, 0.1578947, 0.3157895, 0.4736842, 0.6315789, 0.7894737,
0.9473684, 1.1052632,1.2631579, 1.4210526, 1.5789474, 1.7368421,
1.8947368, 2.0526316, 2.2105263, 2.3684211, 2.5263158 ,
2.6842105, 2.8421053, 3.000000)
f = function(x) (x+1)*(x-2)*(x-1)*(x)*(x+1)*(x-2)*(x-3)
plot(x, f(x), type="l")
maximums = sapply(x, function(x)optimize(f, c(0, x), maximum = TRUE)$maximum)