I have a panel data frame similar to this:
df <- data.frame(
year = c(2012L, 2013L, 2014L, 2015L, 2016L, 2017L, 2012L, 2013L, 2014L, 2015L,
2016L, 2017L),
id = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L),
c = c(7.8L, 8.1L, 51L, 8.2L, 9L, 10L, 7.8L, 8.1L, 8.2L, 0.1L, 9.5L, 10L)
)
df
# year id c
# 1 2012 1 7.8
# 2 2013 1 8.1
# 3 2014 1 51.0
# 4 2015 1 8.2
# 5 2016 1 9.0
# 6 2017 1 10.0
# 7 2012 2 7.8
# 8 2013 2 8.1
# 9 2014 2 8.2
# 10 2015 2 0.1
# 11 2016 2 9.5
# 12 2017 2 10.0
I have certain typo errors in my data where there are huge obvious jumps or declines in the data similar to 51 and 0.1 in column c. I would like to be able to identify these errors within each group and adjust them by taking an average of the value before and after the error. I would like to define these error jumps or declines as anything that is at least more than four or less than a quarter of the surrounding values.