Reputation: 33
I have sequence of the numbers(Really it is just a piece of this sequence. In fact I have over 100k numbers)
1 2 3 3 2 3 2 3 2 1 2 3 2 3 2 3 3 2 3 2 3 2 1 3 3 2 3 3 2 3 3 3 2 3 2 3 2 1 3 2 3 3 3 2 3 3 2 3 2 3
I need to calculate the average number of steps after I get 1 in this sequence. For example: In this sequence 1 is first number. Now I count number of steps to get next 1 and I get 9. Next 1 is after 13 steps, next after 15 steps etc.
Now I have to calculate the average number of steps. So there we have (9+13+15)/3= 12.(3)
How I can do this in R Language?
Upvotes: 1
Views: 78
Reputation: 193527
You can try:
mean(diff(which(x == 1)))
## [1] 12.33333
Given:
x <- c(1, 2, 3, 3, 2, 3, 2, 3, 2, 1, 2, 3, 2, 3, 2, 3, 3, 2, 3, 2,
3, 2, 1, 3, 3, 2, 3, 3, 2, 3, 3, 3, 2, 3, 2, 3, 2, 1, 3, 2, 3,
3, 3, 2, 3, 3, 2, 3, 2, 3)
Upvotes: 5