Reporting missing dates

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Reporting missing dates

reichmaj
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]
 

Sincerely

Jeff Reichman


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reporting missing dates

Jim Lemon-4

Hi Jeff,
As I'm sure you realize, that only tells you whether a date is within
the range that you have specified. Do you only want to find dates
within a certain range:

new_date<-as.Date("2020-01-10")
new_date < min(d) | new_date > max(d)

or maybe whether the text string specifying the date is NA or cannot
be converted to a date?

Jim

On Thu, Jan 16, 2020 at 8:28 AM Jeff Reichman <[hidden email]> 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]
>
>
> Sincerely
>
> Jeff Reichman
>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reporting missing dates

PIKAL Petr
In reply to this post by reichmaj
Your approach is quite simple and works well

Another option is
as.Date(setdiff(date_range, d), origin="1970-01-01")
but I do not believe that it has some advantage above yours.

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Jeff Reichman
> Sent: Wednesday, January 15, 2020 10:28 PM
> To: [hidden email]
> Subject: [R] Reporting missing dates
>
> 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]
>
>
> Sincerely
>
> Jeff Reichman
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reporting missing dates

Duncan Murdoch-2
In reply to this post by reichmaj
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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reporting missing dates

Enrico Schumann-2

Quoting Duncan Murdoch <[hidden email]>:

> 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

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Reporting missing dates

David Winsemius
In reply to this post by reichmaj
Looks reasonable and efficient. There is a seq.Date function that would be implicitly selected by the S3 class dispatch rules  

David

Sent from my iPhone

> On Jan 16, 2020, at 4:28 AM, Jeff Reichman <[hidden email]> 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]
>
>
> Sincerely
>
> Jeff Reichman
>
>
>    [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.