Skip to contents

Detect specific values such as c(0, 100) in vector data and replaces with NA or the local median value.

Usage

replace_invalid(x, values, width, return = c("NA", "median"))

Arguments

x

A numeric vector

values

A numeric vector of values to be replaced, e.g. values = c(0, 100).

width

A numeric scalar for the window length of (2 · width + 1) samples.

return

Indicates whether outliers should be replaced with NA (default) or the local "median" value.

Value

A numeric vector of filtered data.

Details

Useful to overwrite known invalid/nonsense values, such as 0, 100, or 102.3.

TODO: allow for overwriting all values greater or less than known values.

See also

pracma::hampel()

Examples

set.seed(13)
(x <- sample.int(10, 20, replace = TRUE))
#>  [1]  8  3  5 10  6  6  4  8  3  1 10  7  7  5  1  6  8  1  1  4
(y <- replace_invalid(x, values = c(1, 10), width = 5))
#>  [1]  8  3  5 NA  6  6  4  8  3 NA NA  7  7  5 NA  6  8 NA NA  4