ClarityParity ClarityParity - 1 month ago 5
R Question

R - Fraction of matching rows between two columns in a data frame

How can I find out the fraction of rows between two columns match in a data frame?

For example, I have the below data frame:

Col1 Col2
A B
A A
A C
B C
B A
B B
C B
C C
C A


Here, the proportion of matching rows is ~33%

Answer

Something like,

sum(df$Col1 == df$Col2)/nrow(df) * 100

#[1] 33.33333

Counting the values of equal valued columns and dividing it by number of rows.

Comments