R 如何将矢量元素作为一个整体进行匹配

R 如何将矢量元素作为一个整体进行匹配,r,match,R,Match,我想知道如何将向量作为一个整体进行匹配。我有两个向量a,b a <- c(5,1,2,6,3,4,8) b <- c(1,2,3) 在match()上,我获取单个向量元素的位置,对于%中的%我获取单个向量元素的逻辑位置。但我希望将整个向量b立即与a匹配。它不应该匹配单个元素,而应该匹配整个向量,并获得匹配开始的位置 所需输出: 在上面的向量中,未找到匹配项,因为我查找的是整个向量,而不是单个向量项 您可以强制执行此操作,只需逐个元素循环向量 a <- c(5,1,2,6,3,

我想知道如何将向量作为一个整体进行匹配。我有两个向量
a,b

a <- c(5,1,2,6,3,4,8)
b <- c(1,2,3)
match()
上,我获取单个向量元素的位置,对于%中的
%我获取单个向量元素的逻辑位置。但我希望将整个向量
b
立即与
a
匹配。它不应该匹配单个元素,而应该匹配整个向量,并获得匹配开始的位置

所需输出:


在上面的向量中,未找到匹配项,因为我查找的是整个向量,而不是单个向量项

您可以强制执行此操作,只需逐个元素循环向量

a <- c(5,1,2,6,3,4,8)
b <- c(1,2,3)

matchr <- function(a,b){

    # First, loop through the a vector
    for(i in 1:(length(a)-length(b))){

        pos <- FALSE

        # Next loop through the b vector, 
        for(j in 1:length(b)){

            # as we're looping through b, check if each element matches the corresponding part of the a vector we're currently at.
            if( a[i+j-1] == b[j]){
                pos <- TRUE
            } else{
                pos <- FALSE
                break
            }
        }

        # if all the elements match, return where we are in the a vector
        if(pos == TRUE){
            return(i)
        } 
    }
    # if we finish the a vector and never got a match, return no match.
    return("No match")
}

matchr(a,b)
[1] "No match"

d <- c(7,5,4,2,1,2,3,8,5)

matchr(d,b)
[1] 5

e <- c(2,3,8)

matchr(d,e)
[1] 6

同样,通过编译函数,您将获得显著的速度优势。

这是一种方法,有几个示例:

wholematch<-function(a=c(5,1,3,2,1,2,5,6,2,6),b=c(1,2,6))
{
    for(loop.a in 1:(length(a)-length(b)))
    {
    #pmatch gives the first occurrence of each value of b in a. To be sure of finding the consecutive matches, use pmatch starting from all the possible positions of "a"
    wmatch<-(loop.a-1)+pmatch(b,a[loop.a:length(a)])
    #If at any time the number of matches is less than the length of the vector to match, we will never find a match. Return NA 
    if(length(na.omit(pmatch(b,a[loop.a:length(a)])))<length(b)) return(NA)
    #If all indices are adjacent, return the vector of indices
    if(max(diff(wmatch))==1) return(wmatch) #return(wmatch[1]) if you only want the start
    }
}

wholematch()
[1] NA

wholematch(a=c(5,1,3,2,1,2,5,6,2,6),b=c(6,2,6))
[1]  8  9 10

wholematch如果我们根据正在测试的向量检查
match()
输出的长度(使用
na.omit
)如何

ifelse(length(na.omit(match(b, a))) == length(b), match(b, a)[1], NA)
#[1] 2
#adding a new value in b so it wont match, we get
b  <- c(1, 2, 3, 9)
ifelse(length(na.omit(match(b, a))) == length(b), match(b, a)[1], NA)
#[1] NA
ifelse(长度(na.省略(匹配(b,a)))==长度(b),匹配(b,a)[1],na)
#[1] 2
#在b中添加一个新值使其不匹配,我们得到

b@Henrik。谢谢你找到一个可能的副本。我会看一看,可能会有帮助。@rawr感谢您引导链接。这个问题已经结束,提供的链接似乎不相关。以前,后来被删除的
Henrik提供的链接效果良好。谢谢大家@罗尔!!。我指的是顶部的链接,而不是你的链接。请不要删除链接。看起来链接有点混乱。我会重新打开,@rawr可以用他的链接复制它
wholematch<-function(a=c(5,1,3,2,1,2,5,6,2,6),b=c(1,2,6))
{
    for(loop.a in 1:(length(a)-length(b)))
    {
    #pmatch gives the first occurrence of each value of b in a. To be sure of finding the consecutive matches, use pmatch starting from all the possible positions of "a"
    wmatch<-(loop.a-1)+pmatch(b,a[loop.a:length(a)])
    #If at any time the number of matches is less than the length of the vector to match, we will never find a match. Return NA 
    if(length(na.omit(pmatch(b,a[loop.a:length(a)])))<length(b)) return(NA)
    #If all indices are adjacent, return the vector of indices
    if(max(diff(wmatch))==1) return(wmatch) #return(wmatch[1]) if you only want the start
    }
}

wholematch()
[1] NA

wholematch(a=c(5,1,3,2,1,2,5,6,2,6),b=c(6,2,6))
[1]  8  9 10
ifelse(length(na.omit(match(b, a))) == length(b), match(b, a)[1], NA)
#[1] 2
#adding a new value in b so it wont match, we get
b  <- c(1, 2, 3, 9)
ifelse(length(na.omit(match(b, a))) == length(b), match(b, a)[1], NA)
#[1] NA