So I thought I had the answer to my question here: Cumulative sum until maximum reached, then repeat from zero in the next row but in fact it's not.
What I would like to do is be able to sum a column until a given value is reached in another column. If we take for instance:
Col1 Col2 Col3
0 12
0 14
1 2
2 0.5
1 12
4 3
3 2
I'd like to be able to sum all the values of column 2 until we reach 4 in column 1. This would give here: 12+14+2+0.5+12
I'm completely new to R and I sincerely have no idea on how to proceed.
What I have is a data frame from a csv file:
mydata = read.csv("mycsv.csv")
You can use cumsum(mydata$Col1 == 4) == 0
to get a logical vector of whether or not 4 has been reached in Col1. Then you can use simple indexing to grab the relevant elements from Col2:
sum(mydata$Col2[cumsum(mydata$Col1 == 4) == 0])
# [1] 40.5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With