Hi, Let say I have a date variable:> ?asd <- as.Date("2012-01-03") > asd[1] "2012-01-03" Now, I want to express this date like 3/1/2012. can somebody help me how to achieve that? Thanks,
HI, format(asd,"%d/%m/%Y") #[1] "03/01/2012" A.K. ----- Original Message ----- From: Ron Michael <ron_michael70 at yahoo.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Wednesday, December 26, 2012 1:31 PM Subject: [R] Working with date Hi, Let say I have a date variable:> ?asd <- as.Date("2012-01-03") > asd[1] "2012-01-03" Now, I want to express this date like 3/1/2012. can somebody help me how to achieve that? Thanks, ______________________________________________ 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.
try this:> asd <- as.Date("2012-01-03") > asd[1] "2012-01-03"> format(asd, format = '%m/%d/%Y')[1] "01/03/2012">On Wed, Dec 26, 2012 at 1:31 PM, Ron Michael <ron_michael70 at yahoo.com> wrote:> asd <- as.Date("2012-01-03")-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
If you don't have to be finicky about leading zeros, the easy way is: as.character(asd,format="%d/%m/%Y") ?strptime If you are going to be finicky, then asdlt <- as.POSIXlt(asd) with(asdlt,sprintf("%d/%d/%d",mday,mon+1,year+1900)) ?as.POSIXlt ?sprintf --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Ron Michael <ron_michael70 at yahoo.com> wrote:>Hi, > >Let say I have a date variable: > >> ?asd <- as.Date("2012-01-03") >> asd >[1] "2012-01-03" > > > >Now, I want to express this date like 3/1/2012. > >can somebody help me how to achieve that? > >Thanks, > >______________________________________________ >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.