The phrasing in the title is awkward I know. But here goes the free frag:
Assume I have a vector x
. Lets say I want to get the list of values of x[i] - x[i-1]
running down the vector, i.e. rate of change. If you refer to the example, I would want the values of 24.30518 - 24.43082
, and so on, to be saved in a new vector. Is a loop the most effective way of doing so in R?
hour value
1 00 24.43082
2 01 24.30518
3 02 24.18798
4 03 24.07979
5 04 23.99070
6 05 23.91297
7 06 23.84412
8 07 23.83976
9 08 24.37504
10 09 25.24583
11 10 26.22111
12 11 27.14665
13 12 27.85554
14 13 28.34957
15 14 28.53274
16 15 28.39928
17 16 28.03880
18 17 27.51899
19 18 26.91361
20 19 26.16487
21 20 25.52663
22 21 25.10923
23 22 24.79797
24 23 24.58154
Would it be possible to do this with lapply
? Or any other ways such as using a lag? Or any other parsimonious ways?
Bonus question: The data I deal with is a cyclical temperature series, meaning to say that the last element of the vector will be followed by the first element of the vector. This being the case, would it be possible to expand on any of the above methods in order to get a full set of values (instead of a null value for the first element of the output)