Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the index of the second, non-sequential occurrence of a value in a vector using R?

Tags:

r

I need to find the index of the second, non-sequential, occurrence of a value in a vector.

Some example vectors:

Example a) 1 1 1 2 3 4 1 1 1 2 3 4

Example b) 1 2 3 1 1 1 3 5

Note that vectors can have different number of occurrences of each value and are very large (more than 100000 entries)

So, if the value in question is 1, in example a) the result should return the 7th position, and b) should return the 4th.

Thanks in advance for any help or advice you can provide.

Examples Code:

exampleA<-c(1, 1, 1, 2, 3, 4, 1, 1, 1, 2, 3, 4)
exampleB<-c(1, 2, 3, 1, 1, 1, 3, 5)
like image 810
Marcus Morrisey Avatar asked Dec 07 '22 03:12

Marcus Morrisey


1 Answers

The run-length encoding of a vector can be helpful in these sorts of computations:

find.index <- function(x, value) {
  r <- rle(x)
  match.pos <- which(r$value == value)
  if (length(match.pos) < 2) {
    return(NA)  # There weren't two sequential sets of observations
  }
  return(sum(r$length[1:(match.pos[2]-1)])+1)
}

# Test it out
a <- c(1, 1, 1, 2, 3, 4, 1, 1, 1, 2, 3, 4)
b <- c(1, 2, 3, 1, 1, 1, 3, 5)
find.index(a, 1)
# [1] 7
find.index(b, 1)
# [1] 4
find.index(b, 5)
# [1] NA
like image 198
josliber Avatar answered Apr 30 '23 22:04

josliber