[R] difference between 2 dates: IN MONTHS the way Motherscompute it

Gabor Grothendieck ggrothendieck at gmail.com
Sat Mar 11 03:01:55 CET 2006


as.yearmon has a character method so its not necessary to
convert it to Date first:

> print(as.yearmon.character)
function (x, ...)
as.yearmon(as.Date(x, ...))

On 3/10/06, Francisco J. Zagmutt <gerifalte28 at hotmail.com> wrote:
> Gabor, please correct me if I am wrong but shouldn't you use as.Date to
> change the date string to a "Date" class before you call as.yearmon?  i.e.
>
> 12*
> as.numeric(as.yearmon(as.Date("2006-03-07"))-as.yearmon(as.Date("2006-02-07")))
>
> That returns 1 in Windows XP
>
> Regards
>
> Francisco
>
>
>
> >From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
> >To: "Smith, Phil" <pzs6 at cdc.gov>
> >CC: r-help at stat.math.ethz.ch
> >Subject: Re: [R] difference between 2 dates: IN MONTHS the way
> >Motherscompute it
> >Date: Fri, 10 Mar 2006 13:38:12 -0500
> >
> >zoo has the "yearmon" class which will convert a date to a year +
> >0/12 to 11/12 to represent the month, disregarding the day, so:
> >
> >library(zoo)
> >12 * as.numeric((as.yearmon("2006-03-07") - as.yearmon("2006-02-06"))) # 1
> >
> >On 3/10/06, Smith, Phil <pzs6 at cdc.gov> wrote:
> > > Hi R-people:
> > >
> > > I need a function to compute the number of months between 2 dates, in
> >the same way a mother would do it.
> > >
> > > For example, if a kid is born on February 6, the number of months
> >between that date and March 7 is exactly 1 month, although it is only 29
> >days.
> > >
> > > Thank you!
> > > Phil Smith
> > > CDC
> > >
> > >
> > >
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-help
> > > PLEASE do read the posting guide!
> >http://www.R-project.org/posting-guide.html
> > >
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide!
> >http://www.R-project.org/posting-guide.html
>
>
>




More information about the R-help mailing list