Hi, i need a small help. a<- as.Date("2012-01-01") b<- as.Date("2012-12-01") ------------------------------------ My requirement what is, i need to get the month difference of these two dates. Can any one help me please ! - Antony -- View this message in context: http://r.789695.n4.nabble.com/Month-Difference-tp4642483.html Sent from the R help mailing list archive at Nabble.com.
One possibility as.numeric(format(b, "%m"))-as.numeric(format(a, "%m")) Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Rantony > Sent: Friday, September 07, 2012 10:26 AM > To: r-help at r-project.org > Subject: [R] Month Difference > > Hi, > > i need a small help. > > a<- as.Date("2012-01-01") > b<- as.Date("2012-12-01") > ------------------------------------ > My requirement what is, i need to get the month difference of these two > dates. > Can any one help me please ! > - Antony > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Month- > Difference-tp4642483.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Yes. Its working fine. We need to convert to "as.Date" thatz all. Thank you Petr. -----Original Message----- From: PIKAL Petr [mailto:petr.pikal at precheza.cz] Sent: Friday, September 07, 2012 3:42 PM To: Akkara, Antony (GE Energy, Non-GE); r-help at r-project.org Subject: RE: [R] Month Difference One possibility as.numeric(format(b, "%m"))-as.numeric(format(a, "%m")) Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Rantony > Sent: Friday, September 07, 2012 10:26 AM > To: r-help at r-project.org > Subject: [R] Month Difference > > Hi, > > i need a small help. > > a<- as.Date("2012-01-01") > b<- as.Date("2012-12-01") > ------------------------------------ > My requirement what is, i need to get the month difference of these > two dates. > Can any one help me please ! > - Antony > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Month- > Difference-tp4642483.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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 07-09-2012, at 10:26, Rantony wrote:> Hi, > > i need a small help. > > a<- as.Date("2012-01-01") > b<- as.Date("2012-12-01") > ------------------------------------ > My requirement what is, i need to get the month difference of these two > dates.One way is: library(zoo) (as.yearmon(b)-as.yearmon(a))*12 or a.lt <- as.POSIXlt(a) b.lt <- as.POSIXlt(b) b.lt$mon-a.lt$mon Searching in Google with "R date difference months" gives quite a few interesting results. Berend
Le 07/09/12 10:26, Rantony a ?crit :> Hi, > > i need a small help. > > a<- as.Date("2012-01-01") > b<- as.Date("2012-12-01") > ------------------------------------ > My requirement what is, i need to get the month difference of these two > dates. > Can any one help me please ! >Here is a solution from here: http://129.175.106.17/epc/conservation/Girondot/Publications/Blog_r/Entrees/2011/9/18_Nombre_de_mois_entre_deux_dates.html num.months <-function ( x , y ) + { + x<-as.Date( x ) + y<-as.Date( y ) + seeq<-length(seq(from=x , to=y , by="months"))-1 + seeq + } x <- c("1981-12-01") y <- c("1990-05-01") indicemonth <- num.months ( x , y ) Sincerely Marc Girondot -- __________________________________________________________ Marc Girondot, Pr Laboratoire Ecologie, Syst?matique et Evolution Equipe de Conservation des Populations et des Communaut?s CNRS, AgroParisTech et Universit? Paris-Sud 11 , UMR 8079 B?timent 362 91405 Orsay Cedex, France Tel: 33 1 (0)1.69.15.72.30 Fax: 33 1 (0)1.69.15.73.53 e-mail: marc.girondot at u-psud.fr Web: http://www.ese.u-psud.fr/epc/conservation/Marc.html Skype: girondot