[英]R - Automatically split time series in equal parts
I am trying to do a regression mode with calibration periods.我正在尝试使用校准周期进行回归模式。 For that I want to split my time series into 4 equal parts.
为此,我想将我的时间序列分成 4 个相等的部分。
library(lubridate)
date_list = seq(ymd('2000-12-01'),ymd('2018-01-28'),by='day')
date_list = date_list[which(month(date_list) %in% c(12,1,2))]
testframe = as.data.frame(date_list)
testframe$values = seq (1, 120, length = nrow(testframe))
The testframe above is 18 seasons long and I want to devide that into 4 parts, meaning 2 Periodes of 4 winter seasons and 2 Periodes of 5 winter seasons.上面的测试框架有 18 个季节,我想将其分为 4 个部分,即 2 个 4 个冬季的周期和 2 个 5 个冬季的周期。
My try was:我的尝试是:
library(lubridate)
aj = year(testframe[1,1])
ej = year(testframe[nrow(testframe),1])
diff = ej - aj
But when I devide diff now with 4, its 4.5, but I would need something like 4,4,5,5 and use that to extract the seasons.但是当我现在用 4 划分 diff 时,它是 4.5,但我需要像 4,4,5,5 这样的东西并用它来提取季节。 Any idea how to do that automatically?
知道如何自动执行此操作吗?
You can start with something like this:你可以从这样的事情开始:
library(lubridate)
testframe$year_ <- year(testframe$date_list)
testframe$season <- getSeason(testframe$date_list)
If you're wondering the origin of getSeason()
function, read this .如果您想知道
getSeason()
function 的来源,请阅读此。 Now you can split have the datasets with the seasons:现在您可以拆分具有季节的数据集:
by4_1 <- testframe[testframe$year_ %in% as.data.frame(table(testframe$year_))$Var1[1:4],]
by4_2 <- testframe[testframe$year_ %in% as.data.frame(table(testframe$year_))$Var1[5:8],]
by5_1 <- testframe[testframe$year_ %in% as.data.frame(table(testframe$year_))$Var1[9:13],]
by5_2 <- testframe[testframe$year_ %in% as.data.frame(table(testframe$year_))$Var1[14:18],]
Now you can test it, for example:现在您可以对其进行测试,例如:
table(by4_1$year_, by4_1$season)
Fall Winter
2000 14 17
2001 14 76
2002 14 76
2003 14 76
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.