Home > Software design >  How summarise count equal rows between factor columns R?
How summarise count equal rows between factor columns R?

Time:11-11

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

CodePudding user response:

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
  • Related