user3576287
user3576287

Reputation: 1032

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

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?

Upvotes: 0

Views: 2920

Answers (1)

user3576287
user3576287

Reputation: 1032

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

Upvotes: 2

Related Questions