Gaurav Bansal
Gaurav Bansal

Reputation: 5660

Find missing days in R Date sequence

let's say I have a series of dates in R, like so:

d <- as.Date(c('2001-01-01', '2001-01-02', '2001-01-04', '2001-01-05'))

The date 2001-01-03 is missing. Is there a quick way to identify this? In reality I have a much longer series than just 4 observations.

Upvotes: 1

Views: 6848

Answers (1)

phiver
phiver

Reputation: 23598

You could create a range of dates based on the min and max dates in your vector and check with %in% which dates are missing.

d <- c('2001-01-01', '2001-01-02', '2001-01-04', '2001-01-05')
d <- as.Date(d)
date_range <- seq(min(d), max(d), by = 1) 
date_range[!date_range %in% d] 
[1] "2001-01-03"

Upvotes: 8

Related Questions