I am trying to build a for loop with a nested if statement to check for the maximum values in a vector. I hope to take this code and apply it to spectrum data.
Right now it's returning an error that I'm not sure how to troubleshoot. Do I need to break up my logical operators in my If statement?
Code:
r <- c(1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7)
Peaks <- c()
indPeaks <- c()
length(r)
for(x in length(r)) {
if((r[x-1]<r[x] & r[x+1]<r[x])==TRUE){
Peaks <-r[x]
indPeaks <- which(r == r[x])
}
}
Error in if ((r[x - 1] < r[x] & r[x + 1] < r[x]) == TRUE) { : missing value where TRUE/FALSE needed