[R] repeated %in%

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Fri Nov 18 11:39:18 CET 2005


Robin Hankin wrote:

> check.for.inclusion <- function(subsets){
>    out <- rep(FALSE,length(subsets)-1)
>    for(i in 1:(length(subsets)-1)){
>      out[i] <- all(subsets[[i+1]] %in% subsets[[i]])
>    }
>    return(all(out))
> }
> 
> 
> how to do it elegantly and/or quickly?
> 

  My first thought was to rewrite that function but drop out if it didnt 
match. But then...

  Assuming the first list element is the longest, then its a one liner:

check2 <- function(l){
   length(unique(unlist(l))) == length(l[[1]])
}

  - basically the set of everything in all the elements is the first 
element, so take unique(everything) and see if its the same size as the 
first element. Unless I've missed something...

  I'd call that elegant, it may also be quicker!

Baz




More information about the R-help mailing list