[R] Searching elements in list

David Winsemius dwinsemius at comcast.net
Thu Nov 3 21:47:46 CET 2011


On Nov 3, 2011, at 3:21 PM, mstepano wrote:

> I forget to mention that the vectors are ordered. I think that one  
> of the
> possible solutions is to use lapply, i.e.,
>
> < T %in% lapply(allv, function(x,y) all.equal(x,y),y=somev)
> [1] TRUE

all.equal worked fine when the answer was 'true' but not so well in my  
hands when it was 'false'. Also not a good idea to replace the base  
definition of %in%.

 > any( sapply( allv, identical, v1) )
[1] TRUE
 > any( sapply( allv, identical, c(3,3,4)) )
[1] FALSE

`%l.in%` <- function(vec,lis) any( sapply( lis, identical, vec) )

Use:
 > v1 %l.in% allv
[1] TRUE

--

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list