是否可以同时为日期和时间范围设置xts系列的子集?例如,在下面的系列中,我只想选择6:30-6:50和一个月的01-04 (或者更好,一个月的前4个数据日期,但这是一个无关的问题)的行。
spy[,ohlcv]
open high low close volume
2016-05-19 06:30:00 204.030 204.300 203.900 204.100 537530
2016-05-19 06:35:00 204.100 204.340 204.010 204.240 482436
2016-05-19 06:40:00 204.250 204.540 204.240 204.530 441800
...
2016-05-20 06:30:00 204.960 205.250 204.860 205.170 564441
2016-05-20 06:35:00 205.170 205.410 205.170 205.250 593626
2016-05-20 06:40:00 205.260 205.440 205.240 205.350 342840
...我看到了范围选择here和here的一些答案,它们非常有帮助,但没有显示在索引上设置多个并发约束-这将更具可读性。目前我正在通过手工操作来管理这件事。
(temp1 <- as.character(index(spy), format="%H:%M")) >= "06:30" & temp1 <= "06:50" -> set1
as.character(index(spy), format="%d") < "05" -> set2
then, spy[set1 & set2, ]发布于 2016-08-23 09:20:16
没有办法将这两种功能结合起来。您可以做的一件事是使用split将数据分解为每月的数据块,然后使用first获取每个月的前4个观测值。然后,您可以使用time-of-day子设置来获取特定的时间间隔。
require(xts)
times <- seq(as.POSIXct("2016-05-19 04:30:00"),
as.POSIXct("2016-05-20 07:40:00"), by="5 min")
set.seed(21)
x <- xts(rnorm(length(times)), times)
# get the first 4 days' observations for each month
y <- do.call(rbind, lapply(split(x, "months"), first, n="4 days"))
# subset by time interval
y["T06:30:00/T06:50:00"]https://stackoverflow.com/questions/39088002
复制相似问题