I have a data frame. The snippet is:
df1 <- data.frame(x = c(1, 2, 1, 3, 5, 1, 4, 1), y = c(1, 1, 2, 2, 1, 1, 1, 3))
x y
1 1 1
2 2 1
3 1 2
4 3 2
5 5 1
6 1 1
7 4 1
8 1 3
I need to group df1 by y
and sum over x
but accounting for order of y.
I.e I need to create new group after each new y
and sum over corresponding x
.
The desired output is
x y
1 3 1
2 4 2
3 10 1
4 1 3
How to do this in R?