Hi, Try this: dat<-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 ",sep="",header=TRUE,stringsAsFactors=FALSE)
dat$Time<-ifelse(nchar(dat$Time)<4,paste0(0,dat$Time),dat$Time) library(xts) res<-xts(dat[,-c(1,2)],order.by=as.POSIXct(paste(dat[,1],dat[,2],sep=" "),format="%m/%d/%Y %H%M")) res # Open High Low Close Up Down Volume #2012-11-19 09:35:00 137.89 138.06 137.82 138.05 3202541 3013215 0 #2012-11-19 09:40:00 138.04 138.40 138.02 138.38 2549660 2107595 4657255 #2012-11-19 09:45:00 138.38 138.40 138.18 138.19 1627379 1856318 3483697 #2012-11-19 09:50:00 138.20 138.32 138.20 138.30 1046133 896423 1942556 #2012-11-19 09:55:00 138.30 138.49 138.30 138.41 1287646 961604 2249250 #2012-11-19 10:00:00 138.41 138.49 138.39 138.43 806043 731641 1537684 A.K. ----- Original Message ----- From: Costas Vorlow <costas.vor...@gmail.com> To: r-help@r-project.org Cc: Sent: Wednesday, November 28, 2012 4:02 PM Subject: [R] hhmm time format, strptime and %k 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@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. ______________________________________________ R-help@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.