Handle missing values
Usage
ts_replace_missing(ts, value, indexes = NULL)
ts_interpolate_missing(ts, max_missing = 1, indexes = NULL)
ts_fill_missing_dates(ts, start, end, value = NA)Arguments
- ts
a
corehydro_ts.- value
the replacement (or fill) value.
ts_fill_missing_dates()defaults toNA, inserting the absent ordinates as missing rather than as zeros.- indexes
optional 1-based ordinate positions to restrict the operation to.
- max_missing
the longest run of missing values to interpolate across.
- start, end
the date range to fill over.
Details
ts_replace_missing() sets every missing value to value. ts_interpolate_missing() fills a
run of missing values by linear interpolation between its neighbours, but only when the run is
no longer than max_missing; a run reaching the END of the series is EXTRAPOLATED from the two
preceding ordinates instead. Both interpolate in date space, so an irregular spacing is
honoured. ts_fill_missing_dates() is the other half of the problem: it inserts the ordinates a
regular series is missing entirely, over the requested date range.
Examples
ts <- time_series(as.Date("2000-01-01") + 0:4, c(3, NA, 4, 1, 5))
ts_interpolate_missing(ts, max_missing = 1)
#> <corehydro_ts> 5 ordinates, interval "one_day"
#> 2000-01-01 to 2000-01-05
#> 2000-01-01 3
#> 2000-01-02 3.5
#> 2000-01-03 4
#> 2000-01-04 1
#> 2000-01-05 5