I have the following data:
df <- data.frame(
group = c('r1','r2','r3','r4'),
X1 = c('A','B','C','K'),
X2 = c('A','C','M','K'),
X3 = c('D','A','C','K')
)
> df
group X1 X2 X3
1 r1 A A D
2 r2 B C A
3 r3 C M C
4 r4 K K K
I want to estimate a 'similarity score' based on columns X1, X2 & X3. For example, within group r1 (or row 1), 2 out of 3 elements are similar, so score is 2/3 (~67%). And the group r4 (or row 4), the score would be 3/3 (100%). The desired outcome is below:
> df
group X1 X2 X3 similarity_score
1 r1 A A D .67
2 r2 B C A .33
3 r3 C M C .67
4 r4 K K K 1
How can I achieve this?