I have the following dataframe
id = 1:16
vals = c(0,1,1,1,0,0,0,0,1,1,1,0,0,0,1,0)
cumsum = c(0, 1, 2, 3, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 1, 0)
test = data.frame(id,vals, cumsum)
I would like to extract the maximum of test$cumsum for each consecutive sequence. For instance, I can slice the column cumsum of test such that i have the consecutive sequences :
S1 = {0}
S2 = {1,2,3}
S3 = {0,0,0,0}
S4 = {1,2,3}
S5 = {0,0,0}
S6 = {1}
S7 = {0}
As you can see, the zeros slice out my column into different sequences. What i want to return, is the maximum of each non-zero sequence. So I would get
returned_vector <- c(3,3,1)
Where the first entry of the returned_vector is the maximum of S2 (the first non-zero sequence), the second entry of the returned_vector is the maximum of S4 (the second non-zero sequence), the third entry of of the returned_vector is the maximum of S6 (the final non-zero sequence)
I am not sure how I can do it. Basically I just want to return the maximum of all non-zero sequences in my column test$cumsum.
Any help appreciated!
Thanks a lot!