Replaces all missing values with a given value.

na_replace(x, fill = 0, maxgap = Inf)

Arguments

x

Numeric Vector (vector) or Time Series (ts) object in which missing values shall be replaced

fill

Value used to replace the missing values

maxgap

Maximum number of successive NAs to still perform imputation on. Default setting is to replace all NAs without restrictions. With this option set, consecutive NAs runs, that are longer than 'maxgap' will be left NA. This option mostly makes sense if you want to treat long runs of NA afterwards separately.

Value

Vector (vector) or Time Series (ts) object (dependent on given input at parameter x)

See also

Author

Steffen Moritz

Examples

# Prerequisite: Create Time series with missing values x <- ts(c(2, 3, NA, 5, 6, NA, 7, 8)) # Example 1: Replace all NAs with 3.5 na_replace(x, fill = 3.5)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.0 3.0 3.5 5.0 6.0 3.5 7.0 8.0
# Example 2: Replace all NAs with 0 na_replace(x, fill = 0)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2 3 0 5 6 0 7 8
# Example 3: Same as example 1, just written with pipe operator x %>% na_replace(fill = 3.5)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.0 3.0 3.5 5.0 6.0 3.5 7.0 8.0