【发布时间】:2016-03-21 15:59:47
【问题描述】:
我已阅读有关此主题的其他问题,但它们似乎不适用于我的数据集:
Timestamp Bid.price Ask.price Bid.volume Ask.volume
20070313 07:01:04:762 11.14122 11.14478 4.539397 1.891416
20070313 07:01:07:608 11.13930 11.14670 1.277812 3.066750
20070313 07:01:08:701 11.14095 11.14505 0.050396 0.050396
20070313 07:01:11:275 11.14098 11.14502 0.226505 0.543613
20070313 07:01:13:884 11.13930 11.14670 0.322856 0.774855
20070313 07:01:16:588 11.13930 11.14670 0.405654 0.973569
我试过了:
as.POSIXct(fx[,1], format="%y%m%d %H:%M:%S:%OS3")
as.POSIXct(paste(fx[,1]), format="%y%m%d %H:%M:%S:%OS3")
as.POSIXct(paste(fx[,1]), format="%y%m%d %H:%M:%S")
但我得到的只是NAs ...
我该如何解决这个问题?
这是数据集:
fx <- structure(list(Timestamp = c("20070313 07:01:04:762", "20070313 07:01:07:608",
"20070313 07:01:08:701", "20070313 07:01:11:275", "20070313 07:01:13:884",
"20070313 07:01:16:588"), Bid.price = c(11.14122, 11.1393, 11.14095,
11.14098, 11.1393, 11.1393), Ask.price = c(11.14478, 11.1467,
11.14505, 11.14502, 11.1467, 11.1467), Bid.volume = c(4.5393967628479,
1.27781200408936, 0.0503959991037846, 0.226504996418953, 0.3228560090065,
0.405654013156891), Ask.volume = c(1.89141595363617, 3.06675004959106,
0.0503959991037846, 0.543613016605377, 0.774855017662048, 0.973568975925446
)), .Names = c("Timestamp", "Bid.price", "Ask.price", "Bid.volume",
"Ask.volume"), row.names = c(NA, 6L), class = "data.frame")
【问题讨论】:
-
您可以通过
strptime(fx$Timestamp, '%Y%m%d %H:%M:%S:%OS')获得基础知识,但我很确定它会扼杀小数秒,如果它们很重要的话。您可能需要将最后一个:替换为.,以便使用%OS。 -
@alistaire 它正在删除毫秒,根据 OP 的标题,它们似乎很重要。可以使用
%S或%OS,但据我所知不能同时使用两者。我同意,除非使用了一些花哨的包,否则可能有必要删除最后一个:。然后可以使用%Y%m%d %H:%M:%OS,而且可能需要更改options(digits.secs)的设置。 -
合而为一:
strptime(sub(':(\\d{3})$', '.\\1', fx$Timestamp), '%Y%m%d %H:%M:%OS') -
非常感谢@alistaire。在我更改
options(digits.secs=3)之前它不起作用。您可以添加您的答案,以便我接受它