I have a dataframe and i need to automate the slope calculation by lm each 5 rows and jump 1 point and calculate slope with next 5 points and again jump 1 point and calculate slope with 5 points. And if the end of datafra when you not have 5 points put NA or no calculate slope
mtcars[c(1:5),c(1,3)] %>% mutate(slope = lm(c(mpg) ~ c(disp))$coefficients[[2]])
I need to calculate slope to next 5 points with gap = 1
mtcars[c(2:6),c(1,3)] %>% mutate(slope = lm(c(mpg) ~ c(disp))$coefficients[[2]])