# [R] Extracting windows from time series

Jim Lemon drjimlemon at gmail.com
Thu Apr 7 02:19:01 CEST 2016

```Hi Matthias,
It looks to me as though you could do this with a couple of loops:

temps<-rnorm(400,14,0.05)
ttind<-NULL
for(ti in 1:(length(temps)-9)) {
if(temps[ti]-temps[ti+9] >= 0.1 && max(temps[ti]-temps[ti+1:9]) > -0.05)
ttind<-c(ttind,ti)
}
cat("\t\t",paste("Year",1:10,sep=""),"\n")
for(ti in 1:length(ttind)) {
cat("Trend ",ti,":\t",sep="")
cat(round(temps[ttind[ti]:(ttind[ti]+9)],3),sep="\t","\n")
}

Whether my criteria for determining a trend is correct is another matter.

Jim

On Thu, Apr 7, 2016 at 12:27 AM,  <maettuw at students.unibe.ch> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help