[英]Pairwise comparison table from a list R
I have a list of 4 vectors with terms (characters).我有一个包含 4 个带有术语(字符)的向量的列表。 I'm looking to obtain a table with the pairwise comparison of the terms.
我正在寻找一个包含术语成对比较的表格。 How many are equal in each pairwise comparison?
在每对比较中有多少是相等的?
Here is an example:这是一个例子:
set.seed(20190708)
genes <- paste("gene",1:1000,sep="")
x <- list(
A = sample(genes,300),
B = sample(genes,525),
C = sample(genes,440),
D = sample(genes,350)
)
And here is what I'm looking for:这就是我要找的东西:
Those are the number of terms present in both groups.这些是两组中出现的术语数。
We may use outer
if we want a symmetric matrix as output, and as.dist
to present the result as just the lower triangle.如果我们想要一个对称矩阵 output,我们可以使用
outer
,并as.dist
将结果呈现为下三角。
out <- outer(x, x, FUN = Vectorize(function(u, v) length(intersect(u, v))))
as.dist(out)
#> A B C
#> B 151
#> C 128 228
#> D 133 187 150
Or if it is just pairwise comparison without the mirror duplicates或者如果它只是没有镜像重复的成对比较
out <- combn(x, 2, FUN = function(x) length(intersect(x[[1]], x[[2]])))
names(out) <- combn(names(x), 2, FUN = paste, collapse = "_")
stack(out)[2:1]
ind values
1 A_B 151
2 A_C 128
3 A_D 133
4 B_C 228
5 B_D 187
6 C_D 150
Here is another base R option这是另一个基本 R 选项
> crossprod(table(stack(x)))
ind
ind A B C D
A 300 151 128 133
B 151 525 228 187
C 128 228 440 150
D 133 187 150 350
or或者
> as.dist(crossprod(table(stack(x))))
A B C
B 151
C 128 228
D 133 187 150
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.