1

I am trying to do a regression mode with calibration periods. For that I want to split my time series into 4 equal parts.

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.

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. Any idea how to do that automatically?

Mr.Spock
  • 511
  • 2
  • 13
  • you can create a lookup-table with date-ranges you want to divide on, and join them to your data. – Wimpel Oct 04 '19 at 12:59

1 Answers1

1

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. 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
s__
  • 9,270
  • 3
  • 27
  • 45