Hi, I have a column in a dataframe in the form of:> as.vector(SLDATX[1:20])[1] "1/6/1986" "1/17/1986" "2/2/1986" "2/4/1986" "2/4/1986" [6] "2/21/1986" "3/6/1986" "3/25/1986" "4/6/1986" "4/10/1986" [11] "4/23/1986" "4/30/1986" "5/8/1986" "5/29/1986" "6/15/1986" [16] "6/18/1986" "6/23/1986" "6/29/1986" "7/16/1986" "7/25/1986" I'd like to convert it into either yyyy-mm or yyyy/mm form, e.g. 1986-06 or 1986/06, and I've been suggsted to use the strptime() function. However when I look at the documentation of it and tried something like:> strptime(as.vector(SLDATX)[1:20], "%y/%m")[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA I got a bunch of NA's. I also tried:> strptime(as.vector(SLDATX)[1:20], "%y/%m/%d")[1] "2001-06-19" NA "2002-02-19" "2002-04-19" "2002-04-19" [6] NA "2003-06-19" NA "2004-06-19" "2004-10-19" [11] NA NA "2005-08-19" NA NA [16] NA NA NA NA NA It is totally messed up. I'd really appreciate if anyone can point out where I did wrong *_*! Many thanks in advance. -- Cheers, Kevin --------------------------------------------------------------- "Try not. Do, do! Or do not. There is no try" Jedi Master Yoda ---- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
Ko-Kang Kevin Wang wrote:> Hi, > > I have a column in a dataframe in the form of: > >>as.vector(SLDATX[1:20]) > > [1] "1/6/1986" "1/17/1986" "2/2/1986" "2/4/1986" "2/4/1986" > [6] "2/21/1986" "3/6/1986" "3/25/1986" "4/6/1986" "4/10/1986" > [11] "4/23/1986" "4/30/1986" "5/8/1986" "5/29/1986" "6/15/1986" > [16] "6/18/1986" "6/23/1986" "6/29/1986" "7/16/1986" "7/25/1986" >... First, you have to make this character vector into a time object. You want something like: times <- strptime(as.vector(SLDATX[1:20]),"%d/%m/%Y") so R knows what format you're using for dates. From there, format(times,"%Y/%m") will work. Subtle trap - strptime produces a list of 9 vectors; "times" will always have length 9. If you want to include this into a data frame, you'll need to convert to a POSIX time type: as.POSIXct(times) to get the right length. Cheers Jason -- Indigo Industrial Controls Ltd. http://www.indigoindustrial.co.nz 64-21-343-545 jasont at indigoindustrial.co.nz
strptime takes a character input and produces a POSIXlt output so the format you specify to strptime is the format of the input, not the output: format( strptime("10/22/1986", "%m/%d/%Y"), "%Y-%m" ) --- Date: Wed, 26 Nov 2003 13:23:45 +1300 (NZDT) From: Ko-Kang Kevin Wang <kwan022 at stat.auckland.ac.nz> To: R Help <r-help at stat.math.ethz.ch> Subject: [R] strptime Usage Hi, I have a column in a dataframe in the form of:> as.vector(SLDATX[1:20])[1] "1/6/1986" "1/17/1986" "2/2/1986" "2/4/1986" "2/4/1986" [6] "2/21/1986" "3/6/1986" "3/25/1986" "4/6/1986" "4/10/1986" [11] "4/23/1986" "4/30/1986" "5/8/1986" "5/29/1986" "6/15/1986" [16] "6/18/1986" "6/23/1986" "6/29/1986" "7/16/1986" "7/25/1986" I'd like to convert it into either yyyy-mm or yyyy/mm form, e.g. 1986-06 or 1986/06, and I've been suggsted to use the strptime() function. However when I look at the documentation of it and tried something like:> strptime(as.vector(SLDATX)[1:20], "%y/%m")[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA I got a bunch of NA's. I also tried:> strptime(as.vector(SLDATX)[1:20], "%y/%m/%d")[1] "2001-06-19" NA "2002-02-19" "2002-04-19" "2002-04-19" [6] NA "2003-06-19" NA "2004-06-19" "2004-10-19" [11] NA NA "2005-08-19" NA NA [16] NA NA NA NA NA It is totally messed up. I'd really appreciate if anyone can point out where I did wrong *_*! Many thanks in advance. -- Cheers, Kevin --------------------------------------------------------------- "Try not. Do, do! Or do not. There is no try" Jedi Master Yoda ---- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki) ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Thanks! On Wed, 26 Nov 2003, Gabor Grothendieck wrote:> Date: Wed, 26 Nov 2003 00:34:11 -0500 (EST) > From: Gabor Grothendieck <ggrothendieck at myway.com> > To: kwan022 at stat.auckland.ac.nz, r-help at stat.math.ethz.ch > Subject: RE: [R] strptime Usage > > > > strptime takes a character input and produces a POSIXlt output so > the format you specify to strptime is the format of the input, > not the output: > > format( strptime("10/22/1986", "%m/%d/%Y"), "%Y-%m" )It worked perfect. Just out of interest, if I want to convert (either from the original form, i.e. mm/dd/yyyy, or the yyyy-mm form, to quarterly format, e.g.: 1999-1 1999-2 1999-3 1999-4 is it possible to do with strptime? Or do I have to do something creative? ;-D -- Cheers, Kevin --------------------------------------------------------------- "Try not. Do, do! Or do not. There is no try" Jedi Master Yoda ---- Ko-Kang Kevin Wang Master of Science (MSc) Student SLC Tutor and Lab Demonstrator Department of Statistics University of Auckland New Zealand Homepage: http://www.stat.auckland.ac.nz/~kwan022 Ph: 373-7599 x88475 (City) x88480 (Tamaki)
On Mon, 1 Dec 2003, Ko-Kang Kevin Wang wrote:> Thanks! > > On Wed, 26 Nov 2003, Gabor Grothendieck wrote: > > > Date: Wed, 26 Nov 2003 00:34:11 -0500 (EST) > > From: Gabor Grothendieck <ggrothendieck at myway.com> > > To: kwan022 at stat.auckland.ac.nz, r-help at stat.math.ethz.ch > > Subject: RE: [R] strptime Usage > > > > > > > > strptime takes a character input and produces a POSIXlt output so > > the format you specify to strptime is the format of the input, > > not the output: > > > > format( strptime("10/22/1986", "%m/%d/%Y"), "%Y-%m" ) > > It worked perfect. Just out of interest, if I want to convert (either > from the original form, i.e. mm/dd/yyyy, or the yyyy-mm form, to quarterly > format, e.g.: > 1999-1 > 1999-2 > 1999-3 > 1999-4 > is it possible to do with strptime? Or do I have to do something > creative? ;-DWell, strptime converts from strings to internal date format, so it is logically impossible to do this with strptime. As for quarters: how do you define them? Does the quarters() function do what you want? If almost, look at> quarters.POSIXtfunction (x, ...) { x <- (as.POSIXlt(x)$mon)%/%3 paste("Q", x + 1, sep = "") } for ideas. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595