Re: [R] Time arithmetic

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 23 Apr 2008 07:23:06 -0400

library(zoo)
?yearmon

library(chron)
?month.day.year

?strptime

See R News 4/1.

On Wed, Apr 23, 2008 at 6:13 AM, Worik R <worikr_at_gmail.com> wrote:
> I am a bit worried I am reinventing the wheel. Isn't there a calendar
> system in R?
>
> I have written a function to add months to a date and return the number of
> days resulting. I am newish to R so I am hoping there is a package that
> can do this sort of date arithmetic for me...
>
> Worik
>
> DaysInMonths <- function(s,d){
> ## Days in d months from s
> sdate <- MSTD(s)
>
> ## Get day, month and the year
> sd <- as.double(days(sdate))
> sm <- as.double(months(sdate))
> sy <- as.double.difftime(years(sdate))
>
> sm <- sm+d
> while(sm > 12){
> sm <- sm-12
> sy <- sy+1
> }
> sdate2 <- MSTD(paste(sd,sm,sy,sep="/"))
> return(as.double(difftime(sdate2, sdate, units="days")))
> }
>
> MSTD <- function(a){
> if(class(a)[1] == "dates") return (a)
> return(dates(a, format="d/m/y"))
> }
>
> [[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.
>



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 Wed 23 Apr 2008 - 11:32:23 GMT

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 Wed 23 Apr 2008 - 13:30:30 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