Thanks Gabor for your input. However my question is, is your solution general for any value of "a" and "b?"
-----Original Message----- From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Sent: 11 July 2010 00:42 To: Bogaso Christofer Cc: r-help@r-project.org Subject: Re: [R] Need help on date calculation On Sat, Jul 10, 2010 at 3:07 PM, Gabor Grothendieck <ggrothendi...@gmail.com> wrote: > On Sat, Jul 10, 2010 at 3:17 PM, Bogaso Christofer > <bogaso.christo...@gmail.com> wrote: >> Hi all, please see my code: >> >> >> >>> library(zoo) >> >>> a <- as.yearmon("March-2010", "%B-%Y") >> >>> b <- as.yearmon("May-2010", "%B-%Y") >> >>> >> >>> nn <- (b-a)*12 # number of months in between them >> >>> nn >> >> [1] 2 >> >>> as.integer(nn) >> >> [1] 1 >> >> >> >> What is the correct way to find the number of months between "a" and "b", >> still retaining the INTEGER property? > > as.integer truncates so if its slightly below 2, as is the case here, > you will get 1 as you observe so try any of these: > > # 1 > as.integer(round(nm)) > > # 2 - just about any small number will do > as.integer(nm + 0.001) > > Also see: > http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-n umbers-are-equal_003f > # 3 - the mondate class in mondate package stores year/months # as number of months since the instant between the end of 1999 # and start of 2000 library(mondate) as.integer(mondate(b) - mondate(a)) ______________________________________________ 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.