Re: [R] days between dates

From: Ian Gow <iandgow_at_gmail.com>
Date: Mon, 23 May 2011 23:13:57 -0500

Geoffrey:

There may be something for this in one of the packages dealing with dates.

If not, here's one (incomplete) idea, based on something I used for a similar issue a little while ago. Essentially, make a data frame that ranks each weekday over a period in ascending order. This data frame could then be merged in with your data set, with the difference between the "date_num" values for any two dates equalling the number of weekdays between them.

start.date <- as.Date("2006-01-27")
end.date <- as.Date("2007-08-27")

date.list <- seq(from=start.date, to=end.date, by="day")
date.df <- data.frame(date=date.list, weekday=weekdays(date.list))
date.df <- subset(date.df, subset=!(weekday %in% c("Saturday", "Sunday")))
date.df$date_num <- 1:length(date.df$date)
date.df[1:20,]


> date.df[1:20,]
date weekday date_num 1 2006-01-27 Friday 1 4 2006-01-30 Monday 2 5 2006-01-31 Tuesday 3 6 2006-02-01 Wednesday 4 7 2006-02-02 Thursday 5 8 2006-02-03 Friday 6 11 2006-02-06 Monday 7 12 2006-02-07 Tuesday 8 13 2006-02-08 Wednesday 9 14 2006-02-09 Thursday 10 15 2006-02-10 Friday 11 18 2006-02-13 Monday 12 19 2006-02-14 Tuesday 13 20 2006-02-15 Wednesday 14 21 2006-02-16 Thursday 15 22 2006-02-17 Friday 16 25 2006-02-20 Monday 17 26 2006-02-21 Tuesday 18 27 2006-02-22 Wednesday 19 28 2006-02-23 Thursday 20

-Ian

On May 23, 2011, at 12:30 PM, Geoffrey Smith wrote:

> Thank you for the email. The data is unbalanced, meaning that some days are
> missing. So the sequence of days could be something like Tuesday,
> Wednesday, Friday, Monday. The diff function would produce 1, 2, 3. But I
> would like it to produce 1, 2, 1 since there is really only 1 day between
> Friday and Monday. Thank you. Geoff
>
> On Mon, May 23, 2011 at 10:09 AM, Jeff Newmiller
> <jdnewmil_at_dcn.davis.ca.us>wrote:
>

>> Your request seems bizarre to me. If you really want to ignore the actual
>> time intervals, just do your analysis without the actual dates.
>> 
>> If you just want forward-looking time intervals, then put them in the
>> correct index locations.
>> 
>> ?diff
>> 
>> c(diff(DF$DATE),1)
>> ---------------------------------------------------------------------------
>> Jeff Newmiller The ..... ..... Go Live...
>> DCN:<jdnewmil_at_dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
>> Live: OO#.. Dead: OO#.. Playing
>> Research Engineer (Solar/Batteries O.O#. #.O#. with
>> /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
>> ---------------------------------------------------------------------------
>> 
>> Sent from my phone. Please excuse my brevity.
>> 

>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> 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.

        [[alternative HTML version deleted]]



R-help_at_r-project.org mailing list
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. Received on Tue 24 May 2011 - 04:21:06 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 24 May 2011 - 06:30:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive