I'm trying to read some CSV-format financial tick data (source: HISTDATA_COM_ASCII_EURUSD_T_201209.zip) into a zoo series. The data is indexed by a time column which contains timestamps formatted such as 20120902 170010767 - almost like %Y%m%d %H%M%OS3 except milliseconds are not seperated by a decimal point as required by %OS3.

I have attempted to force the required decimal point by dividing the latter (right) half of the timestamp by 1000 and pasting back together again:

FUN <- function(i, format)  {
    sapply(strsplit(i, " "), function(j) strptime(paste(j[1], as.numeric(j[2])/1000), format = format))
}
read.zoo(file, format = "%Y%m%d %H%M%OS3", FUN = FUN, sep = ",")

However, this has not worked - could someone please shed some light on how best to do this properly?

Many thanks

有帮助吗?

解决方案

You could obviously make this shorter but this gives the idea well:

> tm <- "20120902 170010767"    
> gsub("(^........\\s......)(.+$)", "\\1.\\2", tm)
[1] "20120902 170010.767"
> strptime( gsub("(^........\\s......)(.+$)", "\\1.\\2", tm), "%Y%m%d %H%M%OS")
[1] "2012-09-02 17:00:10.767"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top