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.
To count the number of elements of a string, the len() method can be used.
extend(): Adds multiple elements to 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With