How to apply a data exception logic

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.

Something like:

laggedVariable <- dplyr::lag(variable)
newVariable <- ifelse(abs(laggedVariable - variable) <= 5,   laggedVariable, variable)

Great thinking on the lag variable but this doesn't produce the same results.

I think I made a mistake in the write up... I don't want to keep the last value if the new value is within +-5 ..... I essentially want to repeat the last value until the new value is +-5 from the last value.

Thats what you see in my excel columns as well. .... you see alot of repeat values where the data was within the +-.

newVariable <- rep(NA, length(variable))
for (i in 2:length(variable)) {
  newVariable[i] <-
    ifelse(abs(variable[i] - variable[i - 1]) <= 5, newVariable[i - 1], variable[i])
}

But loops are ugly in R, so maybe someone has a better idea.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.