you may not need a function
dat <- c(7,6,6,3,7,9,3,6,6,6,8,9)
fmax <- function(x, vec){
v <- rle(vec)
max(v$lengths[v$values == x])
}
fmax(x=6, vec=dat)
[1] 3
when x in absent from dat
fmax <- function(x, vec){
if(x %in% vec){
v <- rle(vec)
max(v$lengths[v$values == x])
} else 0
}
fmax(x=20, vec=dat)
[1] 0