[R] hhmm time format, strptime and %k

Costas Vorlow costas.vorlow at gmail.com
Wed Nov 28 22:02:41 CET 2012


Hello,

I am having trouble with the conversion specifications as described in
the strptime help page.

> head(dat)
        Date Time   Open   High    Low  Close      Up    Down  Volume
1 11/19/2012  935 137.89 138.06 137.82 138.05 3202541 3013215       0
2 11/19/2012  940 138.04 138.40 138.02 138.38 2549660 2107595 4657255
3 11/19/2012  945 138.38 138.40 138.18 138.19 1627379 1856318 3483697
4 11/19/2012  950 138.20 138.32 138.20 138.30 1046133  896423 1942556
5 11/19/2012  955 138.30 138.49 138.30 138.41 1287646  961604 2249250
6 11/19/2012 1000 138.41 138.49 138.39 138.43  806043  731641 1537684
>

As in the example above,  my data (column vectors in a dataframe) are
time series swhich  have time stamps for hours spanning from 9:35am to
16:00 (column 2).

The hours  are formatted as follows:

935 is 9:35, 940 is 9:40 ... hence
1600 is 16:00.

When I convert the dataframe column for "Close" (dat[,6]  i.e., sixth
column of the dataframe)  to a time series (xts):

using

      SPYhf<-xts(dat[,6], as.POSIXct(paste(dat[,1], dat[,2]), format =
"%m/%d/%Y %H%M"))

I get NAs as indexes for the times where the hours are single digits
(i.e. 9 am).  Thus, correct time stamps start at 10:00 and end at
16:00 (i.e., row 6 of dataframe onwards).

                     [,1]
<NA>                138.05
<NA>                138.38
<NA>                138.19
<NA>                138.30
<NA>                138.41
2012-11-19 10:00:00 138.43

I tried using the %k specifier which the strptime help-page suggests
that it's suitable  when AM hours are described with a single digit
("The 24-hour clock time with single digits preceded by a blank") but
I can not get it to work.

Thanks in advnace for your time and help.
Best,
Costas



More information about the R-help mailing list