【发布时间】:2015-10-27 11:44:00
【问题描述】:
我正在使用 xts 包在 R 中创建示例时间序列。我创建了一个日期范围,以分钟分隔,为每个日期创建示例数据,然后最后聚合小时,汇总数据。这有效,除了一个问题。
完成聚合后,索引不会显示每小时的每个数据,而是显示第 59 分钟的数据。我需要在小时显示索引以合并关注点。以下是我的代码:
#xts simple example code
BD <- chron("01/01/2015", "00:00:00") # Setting begin date.
ED <- chron("02/01/2015", "23:59:00") # Setting end date.
DS <- seq(BD, ED, by = times("00:01:00")) # Creating a sequence of dates seperated by a minute.
data <- runif(length(DS), 0, 100) # Generating random numerical data the length of the date sequence.
x <- xts(data, DS) # Creates an xts object indexed by the dates of "DS" with data from "data".
colnames(x) <- "Data" # Just renaiming the data column in the xts object.
x.agg <- period.apply(x, endpoints(x, "hours"), sum) # Aggregating by hour
我试过这个方法:
index(x.agg) <- index(x.agg) - (1/24/60) * 59
但它在尾部给了我这样的回应:
> index(x.agg) <- index(x.agg) - (1/24/60) * 59
> tail(index(x.agg))
[1] (02/01/15 13:00:00) (02/01/15 14:00:00) (02/01/15 15:00:00) (02/01/15
16:00:00) (02/01/15 17:00:00)
[6] (02/01/15 18:00:00)
上面的整个想法是简单地减去 59 分钟来得到它,但它似乎不起作用。我也尝试过截断和舍入,但它们也给了我奇怪的结果。任何想法将不胜感激!
【问题讨论】:
标签: r time-series aggregation xts