Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A more general function for the number of matching elements among the n elements of a list

Tags:

r

I am trying to calculate the number of common entries among the elements of a list:

temp<-list(element1=c("a","b","c"), element2=c("b","c","d"), 
           element3=c("d","e","f"), element4=c("a","c","z"))

To get the overlap among all pairwise combinations of the elements, this function works:

calculate.overlap.2<-function(y){
  pw<-combn(y,2,FUN=function(x)sum(x[[1]]%in%x[[2]]))
  names(pw)<-combn(y,2,FUN=function(x)paste(names(x)[[1]],names(x)[[2]],sep="-"))
  return(pw)
}

To get the overlap among all three-way combinations of the elements, this function works:

calculate.overlap.3<-function(y){
   pw<-combn(y,3,FUN=function(x)sum(x[[1]]%in%x[[2]]&x[[1]]%in%x[[3]]))
   names(pw)<-combn(y,3,FUN=function(x) paste(names(x)[[1]],names(x)[[2]],names(x)[[3]],sep="-"))
   return(pw)
}

but as you can tell from the numbers inside the function, this is not an elegant solution.

It would be really nice to generalize these two functions into one, and have a function takes as an input the elements in each check of overlap. That is, an input of number.of.elements.per.comparison=2 would be equivalent to calculate.overlap.2 above and an input to the function of number.of.elements.per.comparison=3 would be the same as calculate.overlap.3.

I feel like there is a very elegant solution to this, but I just can't see it.

like image 742
Will Cornwell Avatar asked May 25 '15 07:05

Will Cornwell


People also ask

Which function is used to count the number of elements in a list?

To count the number of elements of a string, the len() method can be used.

Which function can increase the number of elements of a list by more than one?

extend(): Adds multiple elements to a list.

Which function should be used to pair the data elements of a list?

The zip function can be used to extract pairs over the list slicing can be used to successively pair the current element with the next one for efficient pairing.

How do you count a matching element in a list Python?

The len() is used to return total count of match. The enumerate function can be used to produce the key value pair for the list being it's index and value and we can store them using list comprehension. The len() is used to return total count of match.


1 Answers

calculate.overlap <- function(y, i){
  pw <- combn(seq_along(y), i, FUN= function(x) {
    res <- length(Reduce(intersect, y[x]))
    names(res) <- paste(names(y[x]), collapse = "-")
    res
  }, simplify = FALSE)
  do.call(c, pw)
}

calculate.overlap(temp, 3)
#element1-element2-element3 element1-element2-element4 element1-element3-element4 element2-element3-element4 
#                         0                          1                          0                          0 
like image 117
Roland Avatar answered Sep 30 '22 11:09

Roland