user2913161
user2913161

Reputation: 1135

R_ How to find common elements from pairwise comparison and calculate

Can you tell me how to sum the intersected numbers and divide them with size of smaller? (actually, I have tons of lists).

foo = list (X=c("a","b","c"),Y=c("a","d","e","f"),Z=c("a","b","z","f","e"),ZZ=c("b","z","e","c"))

From here,

I want to calculate total number of pairwise common elements from all first and divide the value with the size of smaller one,

that is 3.4166,

3.4166 = 1/3 (intersect(X,Y)/min(X,Y)) + 2/3 (intersect(X,Z)/min(X,Z)) + 2/3 (intersect(X,ZZ)/min(X,ZZ) + 3/4 (intersect(Y,Z)/min(Y,Z)) + 1/4 (intersect(Y,ZZ)/min(Y,ZZ) + 3/4 (intersect(Z,ZZ)/min(Y,ZZ))

Upvotes: 0

Views: 151

Answers (1)

Stephan Kolassa
Stephan Kolassa

Reputation: 8267

A combination of combn and apply may do the trick:

sum(apply(combn(seq_along(foo),2),2,function(xx){
  length(intersect(foo[[xx[1]]],foo[[xx[2]]]))/
    min(c(length(foo[[xx[1]]]),length(foo[[xx[2]]])))
}))

At least this gives the result you have. It is a bit unclear to me where the coefficients (1/3, 2/3, ...) come from... But you may be able to take it from here. Or I can edit if you clarify your question.

Upvotes: 1

Related Questions