[R] repeated %in%

Robin Hankin r.hankin at noc.soton.ac.uk
Fri Nov 18 11:21:27 CET 2005


I have a list of vectors, each one of which should be a subset of the  
previous one.
How do I check that this property actually holds?

Toy problem follows with a list of length 4 but my list can be any  

subsets <- list(
                 l1 = 1:10 ,
                 l2 = seq(from=1,to=9,by=2),
                 l3 = c(3,7),
                 l4 = 3

I need

all(subsets[[4]] %in% subsets[[3]]) &
all(subsets[[3]] %in% subsets[[2]]) &
all(subsets[[2]] %in% subsets[[1]])

I can write a little function to do this:

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]])

how to do it elegantly and/or quickly?

Robin Hankin
Uncertainty Analyst
National Oceanography Centre, Southampton
European Way, Southampton SO14 3ZH, UK
  tel  023-8059-7743

More information about the R-help mailing list