[R] Time arithmetic

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 23 13:23:06 CEST 2008


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.
>



More information about the R-help mailing list