Functional way to reverse cumulative sum?

r

Solution

Just use `diff` to get the successive differences:

> c(vec[1],diff(vec))
 [1]  1  2  3  4  5  6  7  8  9 10

Problem

If I have a vector of a cumulative sum, e.g. ``` > vec <- cumsum(1:10) [1] 1 3 6 10 15 21 28 36 45 55 ``` is there a functional way to translate `vec` into it's original vector of `c(1:10)`? Right now, I'm using a for-loop that goes: ``` > result <- vec[1] > for (i in 2:length(vec)) result <- append(result, vec[i]-vec[i-1]) > result [1] 1 2 3 4 5 6 7 8 9 10 ``` But that doesn't seem very R like to me... Any ideas?

Original source