[R] Reporting missing dates

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Jan 16 11:46:19 CET 2020


On 15/01/2020 4:28 p.m., Jeff Reichman wrote:
> R-help Forum
> 
> I have a 20 year data set and I am looking for a way to find missing dates.
> I wrote this and its works, but am wounding if there is a better way?
> 
> d <- c('2020-01-01', '2020-01-02', '2020-01-04', '2020-01-05')
> d <- as.Date(d)
> date_range <- seq(min(d), max(d), by = 1)
> date_range[!date_range %in% d]

Another approach would be based on diff(d) - 1.  That will count the 
number of missing dates between any pair of dates that are present:

diff(d) - 1
# Time differences in days
# [1] 0 1 0

That shows that the second date is followed by one missing day.

Duncan Murdoch



More information about the R-help mailing list