Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking for sequences in an R vector

Tags:

r

vector

I'm looking for a function or operation such that if I have

A <- c(1, 2, 3, 4, 5)

and B <- c(1, 2, 3)

and C <- c(2, 1)

I'd get a TRUE when checking whether A contained B, and FALSE when checking whether A contained C

basically, the equivalent of the %in% operator but that actually cares about the order of elements

In a perfect world, I'd be able to do this without some kind of apply statement, but I may end up having to

like image 330
dtsavage Avatar asked Sep 20 '25 04:09

dtsavage


2 Answers

Well, if one's allowd to use a kind-of apply loop, then this could work:

"%seq_in%" = function(b,a) any(sapply(1:(length(a)-length(b)+1),function(i) all(a[i:(i+length(b)-1)]==b))) 

(edited thanks to bug-finding by John Coleman!)

EDIT 2: I couldn't resist trying to solve the 'non-contiguous' case, too:

# find_subseq() returns positions within vec of ordered elements of x, or stops with NA upon failing
find_subseq = function(x,vec) {
    p=match(x[1],vec)
    if(is.na(p)||length(x)==1){ p } 
    else { c(p,p+find_subseq(x[-1],vec[-seq_len(p)])) }
}
"%seq_somewhere_in%" = function(b,a) all(!is.na(find_subseq(b,a)))

Examples:

1:3 %seq_in% 1:10
[1] TRUE
c(3,1,2) %seq_in% 1:10
[1] FALSE
c(1,2,3) %seq_in% c(3,2,1,2,3)
[1] TRUE
2:1 %seq_in% c(1,2,1)
[1] TRUE
1:3 %seq_somewhere_in% c(1,10,10,2,10,10,10,3,10)
[1] TRUE
like image 176
Dominic van Essen Avatar answered Sep 21 '25 20:09

Dominic van Essen


Maybe you can define a custom function subseq_check like below

subseq_check <- function(x,y) grepl(toString(y),toString(x),fixed = TRUE)

which gives

> subseq_check(A,B)
[1] TRUE

> subseq_check(A,C)
[1] FALSE

A Hard-core approach

subseq_find <- function(x,y) {
  inds <- which(x == head(y,1))
  if (length(inds)==0) return(FALSE)
  any(sapply(inds, function(k) all(x[k:(k+length(y)-1)]==y)))
}

such that

> subseq_find(A,B)
[1] TRUE

> subseq_find(A,C)
[1] FALSE
like image 45
ThomasIsCoding Avatar answered Sep 21 '25 19:09

ThomasIsCoding