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

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 13 03:13:50 CET 2006


Maybe you have an old version of zoo?  When I run the first one
I get 1.  I am using R 2.2.1 under Windows XP and checking
the Built: line of library(help = zoo) I am using the verson of
zoo version built on Feb 6, 2006:

> 12*as.numeric(as.yearmon("2006-03-07")-as.yearmon("2006-02-07"))
[1] 1

In the second example you are not giving it a date but are subtracting
a bunch of numbers and giving that to as.yearmon.


On 3/12/06, Francisco J. Zagmutt <gerifalte28 at hotmail.com> wrote:
> Thanks Gabor, that's what I understood but then why do I get these results?
>
> library(zoo)
>
> 12*as.numeric(as.yearmon("2006-03-07")-as.yearmon("2006-02-07"))
> [1] NA
> Warning messages:
> 1: NAs introduced by coercion
> 2: NAs introduced by coercion
>
> 12*as.numeric(as.yearmon(2006-03-07)-as.yearmon(2006-02-07))
> [1] -12
>
> 12*as.numeric(as.yearmon(as.Date("2006-03-07"))-as.yearmon(as.Date("2006-02-07")))
> [1] 1
>
> version
>         _
> platform i386-pc-mingw32
> arch     i386
> os       mingw32
> system   i386, mingw32
> status
> major    2
> minor    2.1
> year     2005
> month    12
> day      20
> svn rev  36812
> language R
>
> I am sure I am missing somehting, but what is it??
>
> Thanks
>
> Francisco
>
>
> >From: "Gabor Grothendieck" <ggrothendieck at gmail.com>
> >To: "Francisco J. Zagmutt" <gerifalte28 at hotmail.com>
> >CC: pzs6 at cdc.gov, 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 21:01:55 -0500
> >
> >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