Skip to contents

Detect missing values in vector data and replace via methods from the zoo package.

Usage

replace_missing(
  x,
  method = c("linear", "locf", "spline", "omit"),
  na.rm = FALSE,
  maxgap = Inf
)

Arguments

x

A numeric vector.

method

Indicates how to replace missing data.

"locf"

Stands for 'last observation carried forward'. Replaces each NA with the most recent non-NA value prior to it via zoo::na.locf().

"linear"

Replaces each NA via linear interpolation via zoo::na.approx().

"spline"

Replaces each NA with cubic spline interpolation via zoo::na.spline().

"omit"

Removes NA via stats::na.omit().

na.rm

A logical. If the result of the interpolation still results in leading and/or trailing NAs, should these be removed (using na.trim)?

maxgap

A numeric scalar for the maximum number of consecutive NAs to fill. Any longer gaps will be left unchanged.

Value

A numeric vector of filtered data or a named numeric vector with names from indices of the original input vector.

Details

This function will also replace NaN and Inf to NA.

method = "locf"

if there are no earlier non-NAs, then the NA is either omitted (if na.rm = TRUE) or it is not replaced (if na.rm = FALSE).

method = "linear"

na.rm = TRUE will extrapolate over leading and trailing NAs by applying rule = 2 (see stats::approx()). na.rm = FALSE will return leading/trailing NAs by applying rule = 1.

method = "spline"

TODO

method = "omit"

the returned vector y will be a named vector with the original indices of each value as names. This allows for preserving and re-inserting the omitted NAs back into the final dataset, if needed.

Examples

(x <- c(1, 2, NA, Inf, 5, 6, NA))
#> [1]   1   2  NA Inf   5   6  NA
replace_missing(x, method = "omit")
#> 1 2 5 6 
#> 1 2 5 6 
replace_missing(x, method = "locf")
#> [1] 1 2 2 2 5 6 6
replace_missing(x, method = "linear", na.rm = FALSE)
#> [1]  1  2  3  4  5  6 NA
replace_missing(x, method = "linear", na.rm = TRUE)
#> [1] 1 2 3 4 5 6 6