I am working on a data frame with x and y columns with values as rows.. I want to calculate the slope of x and y for every 2 rows and then using the calculated slope, record whether slope's "stability" is "high" or "low". You'll understand better after seeing the code. What is wrong with this code? When I input stability, R returns NULL.
slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
stability <- c()
for (i in 1:nrow(acc)) {
if (slope[i] > 0 & slope[i] < 0.8) {
stability[i] <- "low"
} else if (slope[i] >= 0.8 & slope[i] <= 1) {
stability[i] <- "high"
} else if (slope[i] > 1 & slope[i] < 1.2) {
stability[i] <- "high"
} else if (slope[i] >= 1.2) {
stability[i] <- "low"
} else if (slope[i] >= -1 & slope[i] <= -0.8) {
stability[i] <- "high"
} else if (slope[i] >= -0.8 & slope[i] <= 0) {
stability[i] <- "low"
} else if (slope[i] < -1 & slope[i] > -1.2) {
stability[i] <- "high"
} else
stability[i] <- "low"
}