Search code examples
rmatchrowcol

Match a vector to multiple consecutive rows in R


I have got a dataframe and I need to find row numbers where the values of the entries in one column match a certain pattern.

Let the col1 col1 = matrix(c(1,0,0,0,0,0,0,0,0,0,2,0,2,0,0,0,0,0,0,0,1), nrow = 21, ncol = 1) be an example of by column and vector r r = c(2, 0 ,2) be a vector I need to match it with.

I need R to return an index number of rows where the pattern in r matches the values in col1 (in this case row 11, 12, 13).

I thought I could achieve this with row.match, but that is not the case. I have tried different combinations of match function, but it doesn't yield any results either.

Maybe the way I am approaching this problem is wrong from the beginning, but I have trouble believing that there isn't any function, that would provide me with the expected result given some adjustment.

Thanks.


Solution

  • You could do this using rollapply from zoo. Basically, this runs identical on a rolling basis with a window of length(r). This tells you that the sequence is present starting at positon 11 of the col1 vector..

    library(zoo)
    which(rollapply(col1,length(r),identical,r))
    [1] 11
    

    To get a vector of positions, you could do:

    which(rollapply(col1,length(r),identical,r))+0:(length(r)-1)
    [1] 11 12 13