[R] strptime Usage

Gabor Grothendieck ggrothendieck at myway.com
Wed Nov 26 06:34:11 CET 2003



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




More information about the R-help mailing list