我试图测试一个短数字向量是否是一个较长的数字向量的一部分.例如,如果a = c(2, 3)和b = c(1, 3, 2, 4, 2, 3, 1),那么我试图找到/想到一个能回答这个问题的函数:是a一部分b吗?输出应该是TRUE.
或者,如果c = c(1, 3, 2, 4, 1, 3, 1)那时输出"是?a的一部分c?" 应该是FALSE.
match() 不做这个工作:
match(a, b)
Run Code Online (Sandbox Code Playgroud)
回报
3 2
Run Code Online (Sandbox Code Playgroud)
%in%运营商也不是:
TRUE TRUE
Run Code Online (Sandbox Code Playgroud)
我知道有字符串匹配的选项,但我不想通过转换为字符串来解决这个问题...
这是我对它的抨击
valInLong <- function(val, long){
n.long <- length(long)
n.val <- length(val)
# Find where in the longer vector the first
# element of val is. This is so we can vectorize later
first <- which(long == val[1])
# If the first element is too near the end we don't care
# about it
first <- first[first <= n.long - n.val + 1]
# sequence from 0 to n.val - 1 used for grabbing subsequences
se <- seq_along(val)-1
# Look at all subsequences starting at 'first' that go
# the length of val and do an elementwise comparison.
# If any match in all positions then the subsequence is
# the sequence of interest.
any(sapply(first, function(x){all(long[x+se] == val)}))
}
long <- rpois(1000, 5)
a <- c(123421, 232, 23423) # probably not in long
valInLong(a, long)
a <- long[34:100]
valInLong(a, long)
Run Code Online (Sandbox Code Playgroud)
这是一次尝试.我不认为它超级快,但它也不是超慢:
a = c(2,3)
b1 = c(1, 3, 2, 4, 2, 3, 1)
b2 = c(1, 3, 2, 4, 1, 3, 1)
ainb <- function(a,b) {
any(apply( embed(b,length(a)), 1, function(x) all(rev(a)==x) ))
}
ainb(a,b1)
#[1] TRUE
ainb(a,b2)
#[1] FALSE
Run Code Online (Sandbox Code Playgroud)