Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How summarise count equal rows between factor columns R?

I have followed data example

df <- tibble(var1 = factor(c("a", "a", "a", "b", "b", "c", "c", "c")),
         var2 = factor(c("a", "b", "b", "c", "c", "c", "d", "d")))

I would like to summarise this data as one row and three columns (1X3) in tibble format. Where the first column show the counting of similar row values, the second column show the counting of different and the third column the total of values with final format as:

final <- tibble(equal = 2, different = 6, total = 8)

thank you all

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use

library(dplyr)
df %>% 
  summarise(equal = sum(as.numeric(var1) == as.numeric(var2)),
            different = sum(as.numeric(var1) != as.numeric(var2)),
            total = n())

This returns

# A tibble: 1 x 3
  equal different total
  <int>     <int> <int>
1     2         6     8
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading