我已阅读有关此主题的其他问题,但它们似乎不适用于我的数据集:
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
Run Code Online (Sandbox Code Playgroud)
我试过了:
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")
Run Code Online (Sandbox Code Playgroud)
但我得到的只是NA...
我该如何解决这个问题?
这是数据集:
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")
Run Code Online (Sandbox Code Playgroud)
因为Timestamp一个有:整秒和分数秒之间,并没有时间解析字符串(“转换说明”,例如%Y为秒的分数)不整,你需要到最后改变:的.,所以你可以解析它%OS。sub可以照顾它,寻找
:\\d{3},捕获(...)$,并将其替换为
.此外,如果您想查看它正在解析的小数秒,您需要设置digits.secs选项:
options(digits.secs = 3)
strptime(sub(':(\\d{3})$', '.\\1', fx$Timestamp), '%Y%m%d %H:%M:%OS')
# [1] "2007-03-13 07:01:04.762 EDT" "2007-03-13 07:01:07.608 EDT" "2007-03-13 07:01:08.700 EDT"
# [4] "2007-03-13 07:01:11.275 EDT" "2007-03-13 07:01:13.884 EDT" "2007-03-13 07:01:16.588 EDT"
Run Code Online (Sandbox Code Playgroud)
请注意,strptime插入本地时区(目前EDT对我而言),但您可以使用其tz参数将其设置为您喜欢的任何内容(或您的数据应该设置的任何内容)。