If you want to use the mondate package you will need to specify the day of the month. Dates "March-2010" and "May-2010" are ambiguous. Recommend you choose last day of both months as "representative days." Then those days will be integer months apart.
> a<-mondate("March 31, 2010", displayFormat="%B %d, %Y") > b<-mondate("May 31, 2010", displayFormat="%B %d, %Y") > print(a) [1] March 31, 2010 > print(b) [1] May 31, 2010 > b-a [1] 2 attr(,"timeunits") [1] "months" > c(b-a) ## strip away the attribute [1] 2 Technically speaking, since "mondates" are fundamentally numeric, the result of b-a is *numeric*, not *integer*, but is as close to the integer 2 as an R *numeric* can be: > is.integer(c(b-a)) [1] FALSE > is.integer(2) [1] FALSE > identical(c(b-a),2) [1] TRUE Even easier, use the mondate.ymd function, which assumes the last day of the month if not provided, and you won't have to worry about the number of days in a month or leap years. You can also retain your "Month-Year" format when printed if that is a requirement: > a<-mondate.ymd(2010,3, displayFormat="%B-%Y") > b<-mondate.ymd(2010,5, displayFormat="%B-%Y") > print(a) [1] March-2010 > print(b) [1] May-2010 > b-a [1] 2 attr(,"timeunits") [1] "months" > identical(c(b-a),2) [1] TRUE This works for any "last-day-of-the-month's" because mondate represents them as numerics with zero fractional part. Hope that helps, Dan Murphy ========================================= Message: 26 Date: Sat, 10 Jul 2010 15:17:07 -0400 From: Gabor Grothendieck <ggrothendi...@gmail.com> To: Bogaso Christofer <bogaso.christo...@gmail.com> Cc: r-help@r-project.org Subject: Re: [R] Need help on date calculation Message-ID: <aanlktimp7z4hhwn-qabdmeeigiyfn0um_aymsn5dx...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jul 10, 2010 at 3:34 PM, Bogaso Christofer <bogaso.christo...@gmail.com> wrote: > Thanks Gabor for your input. However my question is, is your solution > general for any value of "a" and "b?" > #1 and #2 are general. For #3 I think there is a possibility you might in general have to do the same tricks as #1 and #2 but am not sure. Suggest you discuss with author of mondate package. [[alternative HTML version deleted]] ______________________________________________ R-help@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.