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 -- View this message in context: http://www.nabble.com/how-to-transform-m-d-yyyy-to-yyyymmdd--tp24591898p24591898.html Sent from the R help mailing list archive at Nabble.com.
warmstrong at research:~$ R> strptime("12/9/2007","%m/%d/%Y")[1] "2007-12-09"> format(strptime("12/9/2007","%m/%d/%Y"),"%Y%m%d")[1] "20071209">On Tue, Jul 21, 2009 at 1:16 PM, liujb<liujulia7 at yahoo.com> 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 > -- > View this message in context: http://www.nabble.com/how-to-transform-m-d-yyyy-to-yyyymmdd--tp24591898p24591898.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 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 > Juliadates <- 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
Why not convert them to dates? Assuming you are using that perverse Month/Day/Year format x <- c("12/9/2007" ,"12/16/2007","1/1/2008","1/3/2008","1/12/2008") xx <- as.Date(x, "%m/%d/%Y") xx --- On Tue, 7/21/09, liujb <liujulia7 at yahoo.com> wrote:> From: liujb <liujulia7 at yahoo.com> > Subject: [R] how to transform m/d/yyyy to yyyymmdd? > To: r-help at r-project.org > Received: Tuesday, July 21, 2009, 1:16 PM > > 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 > -- > View this message in context: http://www.nabble.com/how-to-transform-m-d-yyyy-to-yyyymmdd--tp24591898p24591898.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. >
Assuming that there are no duplicate dates, try this using zoo and chron:> Lines <- "12/9/2007 0+ 12/16/2007 1 + 1/1/2008 2 + 1/3/2008 3 + 1/12/2008 4"> > library(zoo) > library(chron) > z <- read.zoo(textConnection(Lines), FUN = chron) > z # is automatically sorted by times12/09/07 12/16/07 01/01/08 01/03/08 01/12/08 0 1 2 3 4> plot(z)See R News 4/1 for more about dates and see the 3 vignettes (pdf documents) in the zoo package for manipulating time series with zoo. On Tue, Jul 21, 2009 at 1:16 PM, liujb<liujulia7 at yahoo.com> 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 > -- > View this message in context: http://www.nabble.com/how-to-transform-m-d-yyyy-to-yyyymmdd--tp24591898p24591898.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. >