R-仅细分时间序列中的工作日

问题描述 投票:0回答:1

我只想从taylor包中的时间序列forecast中将工作日作为子集。

help(taylor)
Half-hourly electricity demand in England and Wales from Monday 5 June 2000 to Sunday 27 August 2000.

但是时间序列的时间不是日期,它们是从1开始的数字,表示从序列开始的天:

time(head(taylor))  
Time Series:
Start = c(1, 1) 
End = c(1, 6) 
Frequency = 336 
[1] 1.000000 1.002976 1.005952 1.008929 1.011905 1.014881

如何将它们转换为日期,仅提取工作日样本,并创建频率为5 * 24 * 2(而不是原来的频率7 * 24 * 2)的新时间序列?

r time-series forecasting
1个回答
0
投票

我们可以使用taylor说明中给出的开始日期和结束日期创建日期序列。根据this answer,我们可以使用cycle()将日期与"ts"对象进行匹配。

data("taylor", package="forecast")

dates <- seq(as.POSIXct("2000-06-05"), as.POSIXct("2000-08-27"), "30 min")[cycle(taylor)]

[现在,使用substr(),我们可以排除以weekdays()开头的"S"(可能不适用于其他语言),并使用所需的"ts"startend创建一个新的frequency对象值。

taylor2 <- ts(taylor[!substr(weekdays(dates), 1, 1) == "S"], 1, 12, 240)

检查

op <- par(mfrow=c(2, 1))
plot(taylor)
plot(taylor2)
par(op)

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.