0

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)

Reuben L.
  • 2,806
  • 2
  • 29
  • 45

3 Answers3

3

'diff' gives the lagged differences, with the lag defaulting to 1 (as desired here).

To get the additional element, calculate it directly and prepend to the result of diff:

first <- x$value[1] - x$value[length(x$value)
result <- c(first, diff(x$value))
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
  • thanks! i'm giving danas the vote cos he sorta came first and has less points anyway! – Reuben L. May 28 '12 at 16:03
  • yep i'm one of the upvoters. now head over to my next question and be the fastest finger this time! ;p http://stackoverflow.com/questions/10787640/r-ddply-summarize-with-large-number-of-columns – Reuben L. May 28 '12 at 16:20
2

Avoid loops in R - they are slow. For your specific task use diff.

danas.zuokas
  • 4,551
  • 4
  • 29
  • 39
1

The more general answer to your question is to do the mathematical operation between your vector and a shifted copy of your vector. The diff() function is a special case for just differences but you could use any vectorized function on two shifted vectors. For example, let's say you wanted the products of x and the next item in x.

n <- length(x)
x[1:(n-1)] * x[2:n]

(You might want to pad that to match length of the original vector, probably on the end in this case because the last item can't be multiplied by the next.)

{to directly answer your question in the general case, here's a diff equation

x[2:n] - x[1:(n-1)]

}

John
  • 23,360
  • 7
  • 57
  • 83