How to detect that a vector is subset of specific vector?

后端 未结 3 1531
感情败类
感情败类 2020-12-11 19:26

I have two vectors (sets) like this:

first<-c(1,2,3,4,5)
second<-c(2,4,5)

how can I detect that whether second is subset

相关标签:
3条回答
  • 2020-12-11 19:41

    Here's another

    setequal(intersect(first, second), second)
    ## [1] TRUE
    

    Or

    all(is.element(second, first))
    ## [1] TRUE
    
    0 讨论(0)
  • 2020-12-11 19:55

    If the order of the array elements matters, string conversion could help:

    ord_match <- function(x,y){
        m <- c(0,grep(paste0(x,collapse=""),
                      paste0(y,collapse=""), fixed = T))
        return(as.logical(m)[length(m)])
    }
    
    0 讨论(0)
  • 2020-12-11 20:00

    Here's one way

    > all(second %in% first)
    [1] TRUE
    
    0 讨论(0)
提交回复
热议问题