[R] Time arithmetic

From: Worik R <worikr_at_gmail.com>
Date: Wed, 23 Apr 2008 22:13:27 +1200

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


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. Received on Wed 23 Apr 2008 - 10:28:38 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 - 12: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