I have a data frame that consists of Pressure values and date/Time.

I want to be able to create a logic that lets me apply a +-5 filter function to the pressure column so that if a pressure value is more or less than 5 than the value in the previous row then it stays as is otherwise we change it to the value from the previous row.

Essentially I am filtering out the noise by only capturing the trend of the data by applying a +-5 exception filter.

In Excel I can do this using the IFS function comparing each cell to previous cell and if it is bigger or smaller than 5 it keeps its current value otherwise it replaces the value with the one from the previous cell.

Is there a way to this in R?

The picture attached is a sample of the data to demonstrate how I manipulated it in Excel. The grey is the raw data. The white values have the filter formula applied.