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 viazoo::na.locf()
."linear"
Replaces each
NA
via linear interpolation viazoo::na.approx()
."spline"
Replaces each
NA
with cubic spline interpolation viazoo::na.spline()
."omit"
Removes
NA
viastats::na.omit()
.
- na.rm
A logical. If the result of the interpolation still results in leading and/or trailing
NA
s, should these be removed (using na.trim)?- maxgap
A numeric scalar for the maximum number of consecutive
NA
s 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-
NA
s, then theNA
is either omitted (ifna.rm = TRUE
) or it is not replaced (ifna.rm = FALSE
).method = "linear"
na.rm = TRUE
will extrapolate over leading and trailingNA
s by applyingrule = 2
(seestats::approx()
).na.rm = FALSE
will return leading/trailingNA
s by applyingrule = 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 omittedNA
s 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