Skip to contents

Suppose you have a vector of dates with at least one non-missing value and you know that they either 1) have a consistent sequential time difference from one value to the next, or 2) you have additional information in the form of some numeric date offset. This function will fill in missing dates backwards or forwards. You can either specify sequential differences of a single value, or use a vector of differences to apply to each position.

Usage

date_fill(date, diff, units = "days")

Arguments

date

A vector of dates.

diff

A numeric vector which represents the time differences to apply. Needs to be length 1 (sequential filling by this time difference value) or length(date) (the time difference at position n from n-1).

units

A string for diff units.

Value

Vector of dates

Examples


#----------------------------------------------------------------------------
# date_fill() examples
#----------------------------------------------------------------------------
library(bkmisc)

date <- as.Date(c(NA, NA, "2022-10-05", NA, NA, NA))
diff <-  c(1, 1, 1, 3, 1, 1)
date
#> [1] NA           NA           "2022-10-05" NA           NA          
#> [6] NA          
date_fill(date, diff)
#> [1] "2022-10-03" "2022-10-04" "2022-10-05" "2022-10-08" "2022-10-09"
#> [6] "2022-10-10"