[R] intersect more than two sets

Tony Plate tplate at acm.org
Tue Apr 24 21:22:47 CEST 2007


I don't think there's that sort of "apply-reduce" function in R, but for 
this problem, the last line below happens to be a "one-liner":

 > set.seed(1)
 > x <- lapply(1:10, function(i) sample(letters, 20))
 > table(unlist(x))

  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x 
  y  z
  6  8  7  8  9  9 10  9  8 10  6  7  9  7  6  8  8  6  9  6  9  6  9  7 
  6  7
 > which(table(unlist(x))==10)
  g  j
  7 10
 > names(which(table(unlist(x))==10))
[1] "g" "j"
 >


Weiwei Shi wrote:
> assume t2 is a list of size 11 and each element is a vector of characters.
> 
> the following codes can get what I wanted but I assume there might be
> a one-line code for that:
> 
> t3 <- t2[[1]]
> for ( i in 2:11){
> 	t3 <- intersect(t2[[i]], t3)
> }
> 
> or there is no such "apply"?
> 
> On 4/24/07, Weiwei Shi <helprhelp at gmail.com> 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?
>>
>> --
>> Weiwei Shi, Ph.D
>> Research Scientist
>> GeneGO, Inc.
>>
>> "Did you always know?"
>> "No, I did not. But I believed..."
>> ---Matrix III
>>
> 
>



More information about the R-help mailing list