[R] intersect more than two sets

Charles C. Berry cberry at tajo.ucsd.edu
Tue Apr 24 21:17:45 CEST 2007


On Tue, 24 Apr 2007, Weiwei Shi wrote:

> Hi,
> I searched the archives and did not find a good solution to that.
>
> assume I have 10 sets and I want to have the common character elements of them.
>
> how could i do that?

  list.of.sets <- lapply(1:10,function(x) sample(letters,20)) # for example

  names( which( table( unlist( lapply( list.of.sets, unique ) ) ) ==10 ) )

or

  int.rec <- function(x) {
    if (length(x) == 1 )
      {
        x
       } else {
                x <-
                c(list(intersect(x[[1]],x[[2]])),x[-(1:2)]);Recall(x)}
}

   int.rec(list.of.sets)

>
> -- 
> Weiwei Shi, Ph.D
> Research Scientist
> GeneGO, Inc.
>
> "Did you always know?"
> "No, I did not. But I believed..."
> ---Matrix III
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                        (858) 534-2098
                                          Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	         UC San Diego
http://biostat.ucsd.edu/~cberry/         La Jolla, San Diego 92093-0901



More information about the R-help mailing list