[R] Extracting windows from time series

maettuw at students.unibe.ch maettuw at students.unibe.ch
Wed Apr 6 16:27:33 CEST 2016


Dear R Users

Thanks for the help in advance and lets get straight to the problem:
I have a 400 year long temperature time series and I am looking for decades that show a linear trend decrease of approximately -0.1 Kelvin or degrees. --> What I would like to program: A loop/function / command line that prints the values of all the trends (can also be overlapping) into a matrix that could have the following structure :

                Year 1 (of Trend1)     Year 2 ( ... 2)     Year 3 .....     .....     .....
Trend 1:
Trend 2:
Trend 3:
...
.
.

 I  first tried to solve the task with a loop but ended up doing it with function but got stuck. Here is what I did so far:

Puls <- Surface_temperature_MA10[10:394] + 1 # give all values in the TS a positive value. Like that
## I can easier extract the trends that fullfill the searched condition (decrease of 0.1 Kelvin) --> look  next line

Difference <- diff(Puls,lag=10) # x[(1+lag):n] - x[1:(n-lag)] --> time step 20 - timestep 10 for first calculation --> does that for whole time series
ID <- c(1:375)
melted_Difference <- melt(data.frame(Difference,ID),id.vars="ID")

Hiatus <- subset(melted_Difference,value < -0.1)

here the result :
23   23 Difference -0.1184901
24   24 Difference -0.1723032
25   25 Difference -0.1366112
26   26 Difference -0.1745479
27   27 Difference -0.1805964
28   28 Difference -0.2285250
29   29 Difference -0.2449096
30   30 Difference -0.1052558
44   44 Difference -0.1172029

-->23,24,25 etc. corresponds to the first years of the trends that shows a decrease of at least -0.1 Kelvin/decade. So far my method works. The purpose of that was that I could then use the window() function to extract
the original values from Puls.
This works, but only for one decade at one time -->

List_Hiatus <- window(Puls,1,start = c(23), end =  c(34))
> List_Hiatus
 [1] 1.125813 1.143880 1.123572 1.139369 1.134410 1.137944 1.139320 1.055780 1.026300 1.042695 1.007323 0.971577
attr(,"tsp")
[1] 23 34  1

Tried but failed with the following approaches:

Start_H <- as.numeric(Hiatus[,1:1])
End_H <- as.numeric(Start_H + 10)
List_Hiatus <- window(Puls,1,start = "Start_H", end =  "End_H")

OR :
List_Hiatus <- window(Puls,1,start = c(23,24), end =  c(33,34))   # where c could of course be expanded

Or
apply(Puls,2,window(start=c(Start_H),end=c(End_H)))

Thanks again for your time.

Best Matthias



	[[alternative HTML version deleted]]



More information about the R-help mailing list