Edit: path to regex:
match.regex <- function(x,data){ xs <- paste(x,collapse="_") dats <- apply(data,1,paste,collapse="_") sum(grepl(xs,dats)) } > match.regex(c(1),dat) [1] 3 > match.regex(c(0,0,0),dat) [1] 1 > match.regex(c(1,2),dat) [1] 2 > match.regex(5,dat) [1] 0
Surprisingly, this is faster than the other methods given here, and about twice as fast as my solution below, both on small and large datasets. Regular expressions seem to be optimized:
> benchmark(matching(c(1,2),dat),match.regex(c(1,2),dat),replications=1000) test replications elapsed relative 2 match.regex(c(1, 2), dat) 1000 0.15 1.0 1 matching(c(1, 2), dat) 1000 0.36 2.4
An approach that gives you a number right away and works more vectorially looks like this:
matching.row <- function(x,row){ nx <- length(x) sid <- which(x[1]==row) any(sapply(sid,function(i) all(row[seq(i,i+nx-1)]==x))) } matching <- function(x,data) sum(apply(data,1,function(i) matching.row(x,i)),na.rm=TRUE)
Here you first create a matrix with indexes that move the window along a row of the same length as the vector you want to map. Then these windows are checked for vector. This approach is applied for each row, and the sum of the rows returning TRUE is what you want.
> matching(c(1),dat) [1] 3 > matching(c(0,0,0),dat) [1] 1 > matching(c(1,2),dat) [1] 2 > matching(5,dat) [1] 0