I have a data frame (DF) with several columns, but the target columns are date, index and site. A subset table is here: https://www.dropbox.com/s/48165ey5rsv628c/DATA.csv?dl=0
SITE date index
A 2006.001 0.394
A .. 1.408
A 2015.353 1.295
B 2006.001 0.176
B .. 2.354
B 2015.353 0.417
C 2006.001 0.232
C .. 1.733
C 2015.353 0.653
The Time-Series start in 2006 julian day 1 and end in 2015 jd 353 with 23 observations for year.
INDEX_TS <- ts(DF$index, start = c(2006,1), end = c(2015,23), frequency = 23)
Then i decompose it with stl and obtein the seasonal, trend and remainder for each date.
stl(INDEX_TS, 12)
Call:
stl(x = INDEX_TS, s.window = 12)
Components
Time Series:
Start = c(2008, 18)
End = c(2017, 16)
Frequency = 23
seasonal trend remainder
2006.000 0.244352688 0.9678620 -0.34804205
... ... ... ...
2015.957 0.191399568 1.5224135 0.57215711
To extract to table the seasonal
, trend
and remainder
:
STL12 <- stl(INDEX_TS, 12)
DF_STL <- data.frame(STL12, INDEX_TS$time.series)
But only result in a df with index, seasonal, trend and remainder.
I can do it for each site separatelly, subsetting the DF by each one, but the real DF have many different site names.
The final DF that i need is one with de decompose values for each site, like:
SITE date index seasonal trend remainder
A 2006.001 0.394 x1 y1 z1
A .. 1.408 x2 y2 z2
A 2015.353 1.295 x3 y3 z3
B 2006.001 0.176 x4 y4 z4
B .. 2.354 x5 y5 z5
B 2015.353 0.417 x6 y6 z6
C 2006.001 0.232 x7 y7 z7
C .. 1.733 x8 y8 z8
C 2015.353 0.653 x9 y9 z9