I have a dataset of 0s and 1s and amounts attached to them,
test = data.frame(seq = c(0,0,0,1,1,0,1,0,0), amount = c(91.0, 100.0, 0.0, 4.5, 5.5, 3.0, 23.0, 89.0, 56.0))
seq amount
1 0 91.0
2 0 100.0
3 0 0.0
4 1 4.5
5 1 5.5
6 0 3.0
7 1 23.0
8 0 89.0
9 0 56.0
An event is defined by the first 0 in a subsequence of 0s. I am interested in the number of zeros (count) in each event, as well as the amount sums.
For test above, we would have:
- Event 1: 0 0 0, Amount: 191.0
- Event 2: 0, Amount: 3.0
- Event 3: 0 0, Amount: 145.0
So, I would like to create the following table,
| Event | count | amount |
|------------|--------------|----------|
| 1 | 3 | 191.0 |
| 2 | 1 | 3.0 |
| 3 | 2 | 145.0 |
In an earlier post, @27 ϕ 9 sent me this great suggestion for the Event and count columns.
with(rle(test), data.frame(id = sequence(sum(values == 0)), count = lengths[values == 0]))
But how can I add the amount sums still using rle?