Question

I have a time series data, and I wanted to use a function to return suitably lagged and iterated divided value.

Data:

ID   Temperature value
1   -1.1923333
2   -0.2123333
3   -0.593
4   -0.7393333
5   -0.731
6   -0.4976667
7   -0.773
8   -0.6843333
9   -0.371
10  0.754
11  1.798
12  3.023
13  3.8233333
14  4.2456667
15  4.599
16  5.078
17  4.9133333
18  3.5393333
19  2.0886667
20  1.8236667
21  1.2633333
22  0.6843333
23  0.7953333
24  0.6883333

The function should work like this:

new values : 23ID=value(24)/value(23), 22ID=value(23)/value(22), 21ID=value(22)/value(21), and so forth.

Expected Results:

ID  New Temperature value
1   0.17    
2   2.79    
3   1.24    
4   0.98    
5   0.68    
6   1.55    
7   0.885   
8   0.54    
9   -2.03   
10  2.38    
11  1.68    
12  1.264   
13  1.11    
14  1.083   
15  1.104   
16  0.967   
17  0.72    
18  0.59    
19  0.873   
20  0.69    
21  0.541   
22  1.16    
23  0.86    
24  NAN 
Was it helpful?

Solution

To divide each element of a vector x by its successor, use:

x[-1] / x[-length(x)]

This will return a vector with a length of length(x) - 1. If you really need the NaN value at the end, add it by hand via c(x[-1] / x[-length(x)], NaN).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top