On Sat, Jul 10, 2010 at 3:17 PM, Bogaso Christofer <bogaso.christofer at 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-numbers-are-equal_003f
On Sat, Jul 10, 2010 at 3:07 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Sat, Jul 10, 2010 at 3:17 PM, Bogaso Christofer > <bogaso.christofer at 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-numbers-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))
On Sat, Jul 10, 2010 at 3:34 PM, Bogaso Christofer <bogaso.christofer at 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.
On Jul 10, 2010, at 3:17 PM, Bogaso Christofer 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?Not sure what you mean by _retaining_ the integer property since items of the yearmon class are numeric and are NOT integer. You just multiplied 2/12 by 12 and got a result that was very slightly less than 2. There was a thread yesterday where I offered a possible solution to a request that trunc be slightly less "literal" and allow (2/12)*12 to be "truncated" to 2. trnc3 <- function(x) trunc(x+sign(x)* .Machine$double.eps^0.5) > (b-a)*12 == 2 [1] FALSE > trnc3((b-a)*12) == 2 [1] TRUE -- David Winsemius, MD West Hartford, CT
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? Thanks for your time. [[alternative HTML version deleted]]
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:ggrothendieck at gmail.com] Sent: 11 July 2010 00:42 To: Bogaso Christofer Cc: r-help at r-project.org Subject: Re: [R] Need help on date calculation On Sat, Jul 10, 2010 at 3:07 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Sat, Jul 10, 2010 at 3:17 PM, Bogaso Christofer > <bogaso.christofer at 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))
On Sat, Jul 10, 2010 at 3:17 PM, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:> On Sat, Jul 10, 2010 at 3:34 PM, Bogaso Christofer > <bogaso.christofer at 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. >One difference: #2 assumes b >= a but #1 works regardless of which one is larger.
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 <ggrothendieck@gmail.com> To: Bogaso Christofer <bogaso.christofer@gmail.com> Cc: r-help@r-project.org Subject: Re: [R] Need help on date calculation Message-ID: <AANLkTimP7Z4HHWn-qabDMeEIGIYfN0UM_AYmsn5dX_Rt@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jul 10, 2010 at 3:34 PM, Bogaso Christofer <bogaso.christofer@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]]