[R] how to transform m/d/yyyy to yyyymmdd?

Chuck Cleland ccleland at optonline.net
Tue Jul 21 19:58:18 CEST 2009


On 7/21/2009 1:16 PM, liujb wrote:
> Hello,
> 
> I have a set of data that has a Date column looks like this:
> 12/9/2007
> 12/16/2007
> 1/1/2008
> 1/3/2008
> 1/12/2008
> etc.
> 
> I'd like the date to look something like the follow (so that I could sort by
> date easily).
> 20071209
> 20071216
> 20080101
> 20080103
> 20080112
> 
> How to do it? Thank you very much
> Julia

dates <- c("2/27/1992", "2/27/1992", "1/14/1992",
           "2/28/1992", "2/1/1992")

as.character(as.Date(dates, "%m/%d/%Y"), "%Y%m%d")
[1] "19920227" "19920227" "19920114" "19920228" "19920201"

?as.Date

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894




More information about the R-help mailing list