如何使用R以百分比显示两列的相似性? [英] How to show a similarity of two columns in percentage using R?
问题描述
我有一个简单的问题要存入其中! 我有两个data.frame,我想比较它们并以百分比显示它们的相似性,但是我不知道怎么做!
I have a simple question which I stock in it! I have two data.frames and I want to compare them and show their similarity in percentage but I do not know how!
这是一个简单的例子:
a <- as.matrix(rbinom(10,1,1/2))
b <- as.matrix(rbinom(10,1,1/2))
> a
[,1]
[1,] 1
[2,] 0
[3,] 1
[4,] 0
[5,] 1
[6,] 0
[7,] 1
[8,] 1
[9,] 1
[10,] 0
> b
[,1]
[1,] 1
[2,] 0
[3,] 1
[4,] 1
[5,] 0
[6,] 0
[7,] 0
[8,] 0
[9,] 1
[10,] 0
我知道该表显示了差异/相似性
I know that table shows the differences/similarities
> table(a,b)
b
a 0 1
0 3 1
1 3 3
但是如何计算百分比呢?例如显示a中的值是否与b相似X%?
But how can I calculate the percentage for it? for example to show values in a are X% similar to b?
推荐答案
感谢您的评论,但我的意思是要显示两列之间的总体相似性,在这里,如果您查看这些矩阵,它们具有6个相似的变量:
Thanks for your comments, but what I meant was to show the over all similarity between two columns which in here if you look at these matrices they have 6 variables similar:
,最终的相似度百分比为: 6/10 ==> 60%
and the final similarity in percentage would be: 6/10 ==> 60%
我找到了解决方法:
colSums(a==b)/length(a)*100
[1] 60
这篇关于如何使用R以百分比显示两列的相似性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!