Replaces each missing value by drawing a random sample between two given bounds.

na_random(x, lower_bound = NULL, upper_bound = NULL, maxgap = Inf)

Arguments

x

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

lower_bound

Lower bound for the random samples. If nothing or NULL is set min(x) will be used.

upper_bound

Upper bound for the random samples. If nothing or NULL is set man(x) will be used.

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)

Details

Replaces each missing value by drawing a random sample between two given bounds. The default bounds are the minimum and the maximum value in the non-NAs from the time series. Function uses runif function to get the random values.

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 by random values that are between min and max of the input time series na_random(x)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.000000 3.000000 2.484501 5.000000 6.000000 7.005998 7.000000 8.000000
# Example 2: Replace all NAs by random values between 1 and 10 na_random(x, lower_bound = 1, upper_bound = 10)
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.000000 3.000000 6.406848 5.000000 6.000000 2.414876 7.000000 8.000000
# Example 3: Same as example 1, just written with pipe operator x %>% na_random()
#> Time Series: #> Start = 1 #> End = 8 #> Frequency = 1 #> [1] 2.000000 3.000000 2.044397 5.000000 6.000000 4.798361 7.000000 8.000000