R: find a vector in the list of vectors

I work with R, and my goal is to verify that this vector is in the list of unique vectors.

The list looks like

final_states <- list(c("x" = 5, "y" = 1),
                 c("x" = 5, "y" = 2),
                 c("x" = 5, "y" = 3),
                 c("x" = 5, "y" = 4),
                 c("x" = 5, "y" = 5),
                 c("x" = 3, "y" = 5))

Now I want to check if the given state has a list. For instance:

state <- c("x" = 5, "y" = 3)

As you can see, the vector state is an element of the final_states list. My idea was to test this with% in% operator:

state %in% final_states

But I get this result:

[1] FALSE FALSE

Can someone tell me what is wrong?

Congratulated, Lupi

+4
source share
3 answers

If you just want to determine if a vector is in the list, try

Position(function(x) identical(x, state), final_states, nomatch = 0) > 0
# [1] TRUE

Position() match(), . nomatch = 0 Position > 0, , , state final_states

+6

"final_states" - "", "" list,

final_states %in% list(state)
#[1] FALSE FALSE  TRUE FALSE FALSE FALSE

mapply, , "" "final_states" ( , )

 f1 <- function(x,y) all(x==y)
 mapply(f1, final_states, list(state))
 #[1] FALSE FALSE  TRUE FALSE FALSE FALSE

rbind , , "" "" "m1".

m1 <- do.call(rbind, final_states)
!rowSums(m1!=state[col(m1)])
#[1] FALSE FALSE  TRUE FALSE FALSE FALSE

 m1[,1]==state[1] & m1[,2]==state[2]
 #[1] FALSE FALSE  TRUE FALSE FALSE FALSE

Update

TRUE/FALSE

  any(mapply(f1, final_states, list(state)))
  #[1] TRUE

  any(final_states %in% list(state))
  #[1] TRUE

 list(state) %in% final_states
 #[1] TRUE

"" fmatch fastmatch

 library(fastmatch)
 fmatch(list(state), final_states) >0
 #[1] TRUE

@Richard Sciven base R , , fmatch

 set.seed(295)
 final_states <- replicate(1e6, sample(1:20, 20, replace=TRUE), 
          simplify=FALSE)
 state <- final_states[[151]]


 richard <- function() {Position(function(x) identical(x, state),
              final_states, nomatch = 0) > 0}
 Bonded <- function(){any( sapply(final_states, identical, state) )}
 akrun2 <- function() {fmatch(list(state), final_states) >0}
 akrun1 <- function() {f1 <- function(x,y) all(x==y)
            any(mapply(f1, final_states, list(state)))}

 library(microbenchmark)
 microbenchmark(richard(), Bonded(), akrun1(), akrun2(), 
        unit='relative', times=20L)
 #Unit: relative
 #    expr          min           lq        mean      median          uq
 # richard()     35.22635     29.47587    17.49164    15.66833    14.58235
 # Bonded() 109440.56885 101382.92450 55252.86141 47734.96467 44289.80309
 # akrun1() 167001.23864 138812.85016 75664.91378 61417.59871 62667.94867
 # akrun2()      1.00000      1.00000     1.00000     1.00000     1.00000
  #          max neval cld
  #     14.62328    20 a  
  #  46299.43325    20 b 
  #  63890.68133    20 c
  #      1.00000    20 a  
+4

, , lapply. , identical "" :

> lapply(final_states, identical, state)
[[1]]
[1] FALSE

[[2]]
[1] FALSE

[[3]]
[1] TRUE

[[4]]
[1] FALSE

[[5]]
[1] FALSE

[[6]]
[1] FALSE

:

lapply(final_states, match, state)

... , c (1,2) - .

, , , , any, sapply lapply.

> any( sapply(final_states[-3], identical, state) )
[1] FALSE
> any( sapply(final_states, identical, state) )
[1] TRUE
+3

All Articles