I have a vector that looks like this:
mass<-c(-2, -6, -79, 31, -28, 198, 132, 0, 262, -187, -475, 701, 926)
I need to summarize the following subset of values ββfrom this vector:
- all positive values;
- negative values ββif they precede the vector with a positive value.
So, in the above example of the vector, I would like to exclude -2, -6 and -79 from the sum (they do not correspond to positive values), but include -28, -187 and -475 (since they precede the vector with positive values).
I can judge. positive values ββwith
sum(mass[mass>0])
But I'm not sure how to include only those negative values ββthat match my criteria.
(I have a large number of vectors for which I need to perform a similar operation, and they do not all have the same sequence of negative / positive values, so I also canβt just solve this problem by multiplying the vector to exclude the first three values, so how the number of excluded values ββwill differ depending on the vector).
Thanks so much for any help!
source share