Quoting Duncan Murdoch <murdoch.dun...@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

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to