[R] strptime Usage

Jason Turner jasont at indigoindustrial.co.nz
Wed Nov 26 01:36:24 CET 2003


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




More information about the R-help mailing list