Hi @genetist,
I suggest you first split your columns into "first_letter" and "second_letter", and then do the comparisons to see if they are the same:
# Add to your data to show various extra data combinations
df <- read.delim(sep="|", text=c("
|IND|M1|M2|
|1|A/A|G/G|
|2|T/T|NA|
|3|NA|C/C|
|4|T/C|A/T|
|5|G/T|A/A|
"))
df
#> X IND M1 M2 X.1
#> 1 NA 1 A/A G/G NA
#> 2 NA 2 T/T <NA> NA
#> 3 NA 3 <NA> C/C NA
#> 4 NA 4 T/C A/T NA
#> 5 NA 5 G/T A/A NA
suppressPackageStartupMessages({
library(tidyverse)
})
df %>%
select(!c(1,5)) %>%
separate(col=M1, sep="/", into=c("M1_1", "M1_2")) %>%
separate(col=M2, sep="/", into=c("M2_1", "M2_2")) %>%
mutate(m1 = ifelse(M1_1 == M1_2, 1, 0),
m2 = ifelse(M2_1 == M2_2, 1, 0)) -> out.df
out.df
#> IND M1_1 M1_2 M2_1 M2_2 m1 m2
#> 1 1 A A G G 1 1
#> 2 2 T T <NA> <NA> 1 NA
#> 3 3 <NA> <NA> C C NA 1
#> 4 4 T C A T 0 0
#> 5 5 G T A A 0 1
out.df %>%
select(!c(2,3,4,5))
#> IND m1 m2
#> 1 1 1 1
#> 2 2 1 NA
#> 3 3 NA 1
#> 4 4 0 0
#> 5 5 0 1
Created on 2021-08-06 by the reprex package (v2.0.0)
I hope this is what you wanted since your description was unclear as @pieterjanvc rightly pointed out.