0

I have a vector, X, looking like the following:

X = c(30, 10, 10, 15, 25, 1, 10, 55, 10, 1, 10, 5, 5, 5, 5, 10, 5, 5, 5, 5, 5, 5, 5, 5) 
                                                                ^
                                                                17

I want to make a function where if I input the above vector and a number, n, it shows where the consecutive n starts. In X above, the desired number is 17 if my n is 5 since starting the 17th location, the rest of the vector is all 5.

So, if I want a function, f, such that f(X,5) gives me 17. Can anyone help me on achieve this in the fastest manner?

Frank
  • 66,179
  • 8
  • 96
  • 180
jay2020
  • 451
  • 1
  • 3
  • 12

1 Answers1

2

If we assume that the last element is always 5 (according to OP), we could just reverse the vector, find the first non five, subtract it from the length of the vector and add 2 in order to switch direction back again

length(X) - which.max(rev(X) != 5) + 2
# [1] 17
David Arenburg
  • 91,361
  • 17
  • 137
  • 196