If I have a vector of numbers in R.
numbers <- c(1,1, 2,2,2, 3,3, 4,4,4,4, 1)
I want to return a vector that provides the number of times that value has occurred cumulatively along the vector. I.e.
results <- c(1,2, 1,2,3, 1,2, 1,2,3,4, 3)
We can use ave
and apply the seq_along
by grouping with the 'numbers' vector
ave(numbers, numbers, FUN = seq_along)
#[1] 1 2 1 2 3 1 2 1 2 3 4 3
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