Match a vector to a list of vectors

前端 未结 3 479
情话喂你
情话喂你 2021-01-17 11:35

I have a list of vectors lis which I need to match to another vector vec

lis <- list(c(2,0,0),c(1,1,0), c(1,0,1), c(0,2,0), c(0,         


        
3条回答
  •  花落未央
    2021-01-17 12:08

    sapply(lis, identical, vec)
    # [1] FALSE  TRUE FALSE FALSE FALSE FALSE
    

    Benchmark:

    l1 <- list(1:1000)[rep(1, 10000)]
    v1 <- sample(1000)
    AG <- function() sapply(l1,function(x)all(x==v1))
    J <- function() sapply(l1, identical, v1)
    microbenchmark(AG(), J())
    # Unit: milliseconds
    #  expr      min       lq    median        uq      max neval
    #  AG() 76.42732 84.26958 103.99233 111.62671 148.2824   100
    #   J() 32.14965 37.54198  47.34538  50.93195 104.4036   100
    

提交回复
热议问题