Goodmorning StackOverflow,
I have seen answer to similar question, however, they do not consider the group_ID
and are not efficient enough to be run on massive datasets.
I am struggling to find a solution to the following task:
within the consecutive elements of each group_ID
, recursively compute the difference
with the previous element starting from the second to the last element belonging to that group_ID.
Therefore, considering the following sample data:
data <- data.frame(time = c(1:3, 1:4),
group_ID = c(rep(c("1", "2"), c(3, 4))),
value = c(0, 400, 2000, 0, 500, 2000, 2120))
The expected result of the solution I am trying to find is:
solution_df <- data.frame(time = c(1:3, 1:4),
group_ID = c(rep(c("1", "2"), c(3, 4))),
difference = c(NA, 400, 1600, NA, 500, 1500, 120))
It is critical to bear in mind the dataset is massive and the solution must be efficient.
I hope the question was clear, otherwise please ask for further details.