检查 R 向量中的序列 [英] Checking for sequences in an R vector
问题描述
我正在寻找一个函数或操作,如果我有
I'm looking for a function or operation such that if I have
A <- c(1, 2, 3, 4, 5)
和B <- c(1, 2, 3)
和 C <- c(2, 1)
检查 A 是否包含 B 时得到 TRUE,检查 A 是否包含 C 时得到 FALSE
I'd get a TRUE when checking whether A contained B, and FALSE when checking whether A contained C
基本上相当于 %in% 运算符,但实际上关心元素的顺序
basically, the equivalent of the %in% operator but that actually cares about the order of elements
在一个完美的世界中,我可以在没有某种apply
语句的情况下做到这一点,但我最终可能不得不
In a perfect world, I'd be able to do this without some kind of apply
statement, but I may end up having to
推荐答案
好吧,如果允许使用某种应用循环,那么这可以工作:
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)))
(由于 John Coleman 的错误发现而编辑!)
(edited thanks to bug-finding by John Coleman!)
编辑 2:我也忍不住尝试解决不连续"的情况:
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)))
示例:
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
这篇关于检查 R 向量中的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!