读取没有小数点格式的毫秒刻度数据到动物园系列

时间:2021-05-17 17:11:58

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.

我正在尝试将一些CSV格式的财务计分数据(来源:HISTDATA_COM_ASCII_EURUSD_T_201209.zip)读入动物园系列。数据由时间列索引,该时间列包含格式化的时间戳,例如20120902 170010767 - 几乎像%Y%m%d%H%M%OS3,除了毫秒不按%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:

我试图通过将时间戳的后一半(右)除以1000并再次粘贴在一起来强制所需的小数点:

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

非常感谢

1 个解决方案

#1


1  

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"

#1


1  

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"