Fill missing date values in a vector
date_fill.RdSuppose 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.
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"