[R] Reporting missing dates

Enrico Schumann e@ @end|ng |rom enr|co@chum@nn@net
Thu Jan 16 12:02:02 CET 2020


Quoting Duncan Murdoch <murdoch.duncan using gmail.com>:

> 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

But you might want to check if the dates in 'd' are really sorted.

-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list