[R] How to transform the date format as "20050425"
Chris Evans
chris at psyctc.org
Mon Apr 25 22:37:29 CEST 2005
Ooops, ditch my clumsy suggestion. Shows what I'm learning from being
on r-help! Thanks Marc and everyone else!
Chris
On 25 Apr 2005 at 15:32, Marc Schwartz wrote:
> On Mon, 2005-04-25 at 15:18 -0500, Yong Wang wrote:
> > Dear R user,
> > if the dates are in format as "20050425" i.e., Apr. 25 2004"
> > can you suggest an easy way to transfom it to standard form
> > as "2005-04-25" or "2004Apr25" or "2005/04/25" or any other
> > format which is R recognizable?
> > if there is no easy way to do that, can you let me know what
> > is the function in R performing similiar function as the "string"
> > function in C or some other more basic language, so I can loop
> > through all dates to make the desired change. thank you regards
>
> If you are just formatting the Date (ie. no time component) then look
> at ?as.Date, which has a format argument:
>
> > as.Date("20050425", "%Y%M%d")
> [1] "2005-04-25"
>
> Using the format argument, you then return a standard Date class
> object for use in R. The format argument varies as required for your
> data format.
>
> On the output side, there is the format() function, which has a method
> for Dates, that you can then use to convert the Date class object into
> a character vector:
>
> > MyDate <- as.Date("20050425", "%Y%M%d")
> > MyDate
> [1] "2005-04-25"
>
>
> > format(MyDate, "%B %d %Y")
> [1] "April 25 2005"
>
> Also see ?strftime for details on date format strings.
>
> HTH,
>
> Marc Schwartz
>
> ______________________________________________
> 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
--
Chris Evans <chris at psyctc.org>
Consultant Psychiatrist in Psychotherapy, Rampton Hospital;
Research Programmes Director, Nottinghamshire NHS Trust,
Hon. SL Institute of Psychiatry
*** My views are my own and not representative of those institutions
***
More information about the R-help
mailing list