I have a similar question to the question found here: R, subtract value from previous row, group by (slight modification; see below):
In R, lets say I have this da
Using base R
, you could do something like this.
res <- lapply(split(Data, Data$id), function(x) {
x$diff <- x$value - x$value[1]
x})
res <- do.call(rbind, res)
row.names(res) <- NULL
res
id date value diff
1 2380 10/30/12 21.01 0.00
2 2380 10/31/12 22.04 1.03
3 2380 11/1/12 22.65 1.64
4 2380 11/2/12 23.11 2.10
5 20100 10/30/12 35.21 0.00
6 20100 10/31/12 37.07 1.86
7 20100 11/1/12 38.17 2.96
8 20100 11/2/12 38.97 3.76
9 20103 10/30/12 57.98 0.00
10 20103 10/31/12 60.83 2.85