hhmm time format, strptime and %k
Try this (prepend leading zero in some cases)
x <- read.table(text = " 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" + , header = TRUE + , as.is = TRUE + )
# make sure there is a leading zero on the time
x$Time <- ifelse(nchar(x$Time) == 3, paste0('0', x$Time), x$Time)
# now convert
x$newTime <- as.POSIXct(paste(x$Date, x$Time), format = "%m/%d/%Y %H%M")
x
Date Time Open High Low Close Up Down Volume
newTime
1 11/19/2012 0935 137.89 138.06 137.82 138.05 3202541 3013215 0
2012-11-19 09:35:00
2 11/19/2012 0940 138.04 138.40 138.02 138.38 2549660 2107595 4657255
2012-11-19 09:40:00
3 11/19/2012 0945 138.38 138.40 138.18 138.19 1627379 1856318 3483697
2012-11-19 09:45:00
4 11/19/2012 0950 138.20 138.32 138.20 138.30 1046133 896423 1942556
2012-11-19 09:50:00
5 11/19/2012 0955 138.30 138.49 138.30 138.41 1287646 961604 2249250
2012-11-19 09:55:00
6 11/19/2012 1000 138.41 138.49 138.39 138.43 806043 731641 1537684
2012-11-19 10:00:00
On Wed, Nov 28, 2012 at 4:02 PM, Costas Vorlow <costas.vorlow at gmail.com> wrote:
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
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.