How to show a similarity of two columns in percentage using R?

跟風遠走 提交于 2020-01-25 22:28:11

问题


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!

Here is a simple example:

   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

But how can I calculate the percentage for it? for example to show values in a are X% similar to b?


回答1:


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:

and the final similarity in percentage would be: 6/10 ==> 60%

I found the solution:

colSums(a==b)/length(a)*100
[1] 60


来源:https://stackoverflow.com/questions/33038287/how-to-show-a-similarity-of-two-columns-in-percentage-using-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!