r - R_ How to find common elements from pairwise comparison and calculate -
can tell me how sum intersected numbers , split them size of smaller? (actually, 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,
want calculate total number of pairwise mutual elements first , split value size of smaller one, 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))
a combination of combn
, apply
may 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 to the lowest degree gives result have. bit unclear me coefficients (1/3, 2/3, ...) come from... may able take here. or can edit if clarify question.
r
No comments:
Post a Comment