Hi R Community: I want to take the difference in two dates: dt2 - dt1. But, I want the answer in months between those 2 dates. Can you advise me? Please respond to: pzs6 at cdc.gov Thank you! Phil Smith Centers for Disease Control and Prevention
On Mon, 02 Nov 2009 06:29:54 -0500 Phil Smith <philipsmith at alumni.albany.edu> wrote:> I want to take the difference in two dates: > > dt2 - dt1. > > But, I want the answer in months between those 2 dates.What do you mean by 'months'? The number of days in a month vary, so the number of months between two dates is not defined. But if you're willing to think of a month as 30 days, you just have to divide by 30: d=dt2-dt1 d/30 or as.numeric(d)/30 if you want to avoid the (wrong) textual description. -- Karl Ove Hufthammer
Since months have different lengths the difference in months is not well defined but lets define it as the difference between the first of the month of two dates. In that case we can use as.yearmon of the zoo package. It converts to year/months, dropping days, using an internal representation of year + 0 for Jan, year + 1/12 for Feb, year + 2/12 for Mar, etc. so differencing and multiplying by 12 gives the number of months:> # test data > d1 <- Sys.Date(); d1[1] "2009-11-02"> d2 <- d1 - 100; d2[1] "2009-07-25"> library(zoo) > 12 * (as.yearmon(d1) - as.yearmon(d2))[1] 4 On Mon, Nov 2, 2009 at 6:29 AM, Phil Smith <philipsmith at alumni.albany.edu> wrote:> Hi R Community: > > I want to take the difference in two dates: > > dt2 - dt1. > > But, I want the answer in months between those 2 dates. > > Can you advise me? > > Please respond to: pzs6 at cdc.gov > > Thank you! > Phil Smith > Centers for Disease Control and Prevention > > ______________________________________________ > R-help at 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. >
On Mon, 2 Nov 2009, Phil Smith wrote:> Hi R Community: > > I want to take the difference in two dates: > > dt2 - dt1. > > But, I want the answer in months between those 2 dates. > > Can you advise me?How long is a month? difftime() can give you an answer in days, which might suffice if you define a month as a number of days. Another idea is to use (untested) getMonth <- function(x) { xx <- as.POSIXlt(x) 12*xx$year + xx$mon + (xx$mday-1)/31 } which will convert to a (fractional) number of months since 1900-01-01, and that can be differenced.> Please respond to: pzs6 at cdc.gov > > Thank you! > Phil Smith > Centers for Disease Control and Prevention-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Apparently Analagous Threads
- US county map question
- difference between 2 dates: IN MONTHS the way Mothers compute it
- state map question
- selecting columns from a data frame or data table by type, ie, numeric, integer
- selecting columns from a data frame or data table by type, ie, numeric, integer