我有以下名为 trades
的 xts 对象:
trades = structure(c("37119712", "37119713", "37119714", "37119715", "37119716",
"37119717", "37119720", "37119722", "37119724", "37119725", "37119726",
"37119727", "37119732", "37119734", "37119735", "37119741", "37119742",
"37119756", "37119774", "37119775", "37119834", "37119835", "37119836",
"37119850", "37119851", "37119852", "37119853", "37119854", "37119855",
"37119856", "BID", "BID", "BID", "BID", "BID", "ASK", "BID",
"BID", "ASK", "BID", "BID", "ASK", "BID", "BID", "ASK", "BID",
"BID", "ASK", "BID", "BID", "ASK", "ASK", "ASK", "BID", "BID",
"ASK", "ASK", "ASK", "BID", "BID", "440.002", "440.002", "440.030",
"440.983", "441.500", "440.000", "440.000", "440.000", "439.335",
"440.998", "441.496", "439.335", "441.496", "441.496", "441.496",
"441.490", "441.500", "439.736", "439.736", "441.400", "439.645",
"439.643", "439.642", "441.400", "441.400", "441.400", "441.400",
"440.960", "441.499", "441.500", "1.0000000", "8.7381500", "1.5481100",
"0.0639222", "0.4908570", "3.1680000", "0.0473160", "0.7825840",
"0.0108432", "0.0270000", "0.0263241", "0.0100000", "0.1060000",
"0.1879220", "0.1150000", "0.1000000", "1.8170000", "0.0222000",
"0.0234469", "2.4815500", "0.2730000", "0.0380000", "3.0852800",
"0.5824950", "0.0100000", "0.0689000", "0.2188070", "0.0100000",
"0.0705000", "0.8139830", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE",
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE",
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE",
"TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE",
"TRUE"), .Dim = c(30L, 5L), .Dimnames = list(NULL, c("id", "type",
"price", "size", "api")), index = structure(c(1399673507, 1399673524,
1399673524, 1399673524, 1399673524, 1399673531, 1399673553, 1399673565,
1399673605, 1399673609, 1399673609, 1399673619, 1399673645, 1399673677,
1399673686, 1399673721, 1399673721, 1399673764, 1399673810, 1399673810,
1399674172, 1399674172, 1399674172, 1399674333, 1399674333, 1399674335,
1399674353, 1399674368, 1399674368, 1399674368), tzone = "CET",
tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"),
.indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"),
.indexTZ = "CET", tzone = "CET")
时间序列不是等距的。当我输入以下命令时:
last(trades["/2014-05-10 00:26:08"], "5 minutes")
我会 expext 从时间间隔(2014-05-10 00:26:08 少于 5 分钟,2014-05-10 00:26:08)中获取所有数据。
但是,我得到以下结果:
id type price size api
2014-05-10 00:15:21 "37119741" "BID" "441.490" " 0.10000000" "TRUE"
2014-05-10 00:15:21 "37119742" "BID" "441.500" " 1.81700000" "TRUE"
2014-05-10 00:16:04 "37119756" "ASK" "439.736" " 0.02220000" "TRUE"
2014-05-10 00:16:50 "37119774" "BID" "439.736" " 0.02344690" "TRUE"
2014-05-10 00:16:50 "37119775" "BID" "441.400" " 2.48155000" "TRUE"
2014-05-10 00:22:52 "37119834" "ASK" "439.645" " 0.27300000" "TRUE"
2014-05-10 00:22:52 "37119835" "ASK" "439.643" " 0.03800000" "TRUE"
2014-05-10 00:22:52 "37119836" "ASK" "439.642" " 3.08528000" "TRUE"
2014-05-10 00:25:33 "37119850" "BID" "441.400" " 0.58249500" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" " 0.01000000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" " 0.06890000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" " 0.21880700" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" " 0.01000000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" " 0.07050000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" " 0.81398300" "TRUE"
但是输入这个:
last(trades["/2014-05-10 00:26:08"], "2 minutes")
给出正确的结果:
id type price size api
2014-05-10 00:25:33 "37119850" "BID" "441.400" " 0.58249500" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" " 0.01000000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" " 0.06890000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" " 0.21880700" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" " 0.01000000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" " 0.07050000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" " 0.81398300" "TRUE"
但话又说回来,命令:
last(trades["/2014-05-10 00:26:08"], "120 seconds")
应该给出与上面相同的结果,但事实并非如此。
钓点在哪里??
最佳答案
如果您的数据不规则, last.xts
似乎不适用于字符 n
。也许尝试这样的事情?
> trades[paste(end(trades)-60*5, end(trades), sep="/"),]
id type price size api
2014-05-10 00:22:52 "37119834" "ASK" "439.645" "0.2730000" "TRUE"
2014-05-10 00:22:52 "37119835" "ASK" "439.643" "0.0380000" "TRUE"
2014-05-10 00:22:52 "37119836" "ASK" "439.642" "3.0852800" "TRUE"
2014-05-10 00:25:33 "37119850" "BID" "441.400" "0.5824950" "TRUE"
2014-05-10 00:25:33 "37119851" "BID" "441.400" "0.0100000" "TRUE"
2014-05-10 00:25:35 "37119852" "ASK" "441.400" "0.0689000" "TRUE"
2014-05-10 00:25:53 "37119853" "ASK" "441.400" "0.2188070" "TRUE"
2014-05-10 00:26:08 "37119854" "ASK" "440.960" "0.0100000" "TRUE"
2014-05-10 00:26:08 "37119855" "BID" "441.499" "0.0705000" "TRUE"
2014-05-10 00:26:08 "37119856" "BID" "441.500" "0.8139830" "TRUE"
关于r - 在 R 中子集 xts,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23595432/