I have this example dataset:
df <- data.frame(ID = c(1, 1, 1, 2, 2, 2), A = c("2018-10-12",
"2018-10-12", "2018-10-13", "2018-10-14", "2018-10-15", "2018-10-16"),
B = c(1, 5, 7, 2, 54, 202))
ID A B
1 1 2018-10-12 1
2 1 2018-10-12 5
3 1 2018-10-13 7
4 2 2018-10-14 2
5 2 2018-10-15 54
6 2 2018-10-16 202
What I'm trying to do is create a column C that is the sum of B but only for dates before each respective row. For instance, the output I'm seeking is:
ID A B C
1 1 2018-10-12 1 1
2 1 2018-10-12 5 6
3 1 2018-10-13 7 13
4 2 2018-10-14 2 2
5 2 2018-10-15 54 56
6 2 2018-10-16 202 258
I generally will use subsets to do individual sumifs when I have those questions, but I'm not sure how to do this in a new column.
My end goal is to determine the dates that each ID (if applicable) crosses 50.
Thanks!