Removes or imputes NA cases in time series lists. Imputation is done via interpolation against time via zoo::na.approx(), and if there are still leading or trailing NA cases after NA interpolation, then zoo::na.spline() is applied as well to fill these gaps. Interpolated values are forced to fall within the observed data range.
This function supports a parallelization setup via future::plan(), and progress bars provided by the package progressr.
Usage
tsl_handle_NA(tsl = NULL, na_action = c("impute", "omit"))
tsl_Inf_to_NA(tsl = NULL)
tsl_NaN_to_NA(tsl = NULL)Arguments
- tsl
(required, list) Time series list. Default: NULL
- na_action
(required, character) NA handling action. Available options are:
"impute" (default): NA cases are interpolated from neighbors as a function of time (see
zoo::na.approx()andzoo::na.spline())."omit": rows with NA cases are removed.
See also
Other tsl_management:
tsl_burst(),
tsl_colnames_clean(),
tsl_colnames_get(),
tsl_colnames_prefix(),
tsl_colnames_set(),
tsl_colnames_suffix(),
tsl_count_NA(),
tsl_diagnose(),
tsl_join(),
tsl_names_clean(),
tsl_names_get(),
tsl_names_set(),
tsl_names_test(),
tsl_ncol(),
tsl_nrow(),
tsl_repair(),
tsl_subset(),
tsl_time(),
tsl_to_df()
Examples
#tsl with NA cases
tsl <- tsl_simulate(
na_fraction = 0.25
)
tsl_count_NA(tsl = tsl)
#> $A
#> [1] 116
#>
#> $B
#> [1] 103
#>
if(interactive()){
#issues warning
tsl_plot(tsl = tsl)
}
#omit NA (default)
#--------------------------------------
#original row count
tsl_nrow(tsl = tsl)
#> $A
#> [1] 93
#>
#> $B
#> [1] 83
#>
#remove rows with NA
tsl_no_na <- tsl_handle_NA(
tsl = tsl,
na_action = "omit"
)
#count rows again
#large data loss in this case!
tsl_nrow(tsl = tsl_no_na)
#> $A
#> [1] 19
#>
#> $B
#> [1] 18
#>
#count NA again
tsl_count_NA(tsl = tsl_no_na)
#> $A
#> [1] 0
#>
#> $B
#> [1] 0
#>
if(interactive()){
tsl_plot(tsl = tsl_no_na)
}
#impute NA with zoo::na.approx
#--------------------------------------
#impute NA cases
tsl_no_na <- tsl_handle_NA(
tsl = tsl,
na_action = "impute"
)
#count rows again
#large data loss in this case!
tsl_nrow(tsl = tsl_no_na)
#> $A
#> [1] 93
#>
#> $B
#> [1] 83
#>
if(interactive()){
tsl_plot(tsl = tsl_no_na)
}
