Search code examples
rcumsum

From a cumulative series, how to get the original series?


My question may sound trivial. but I couldn't find from a cumulative series, how to get the non-cumulative original series in R.

Searched related question & solution in stackoverflow but had no luck.

If I am having a series x <- c(10, 30, 60, 100); this is a cumulative series. I know in R, cumsum(c(10,20,30,40)) will give me the cumulative series. But from that generated cumulative series, how can I get my original series back?


Solution

  • You can use diff, and keeping the first value, you get the original values back, i.e.

    x <- c(10, 30, 60, 100)
    diff(x)
    #[1] 20 30 40
    c(x[1], diff(x))
    #[1] 10 20 30 40