I have a 5845*1095 (rows*columns) data frame that looks like this:
9 286593 C C/C C/A A/A
9 334337 A A/A G/A A/A
9 390512 C C/C C/C C/C
c <- c("9", "286593", "C", "C/C", "C/A", "A/A")
d <- c("9", "334337", "A", "A/A", "G/A", "A/A")
e <- c("9", "390512", "C", "C/C", "C/C", "C/C")
dat <- data.frame(rbind(c,d,e))
I want the values in the third column to be used to change the columns to its right so if (per row 1) column 3 is "C", then column 4 is turned from "C/C" to "0" as it has the same letter. One letter match is "1" (can be first or second letter) and no letter match is "2" .
9 286593 C 0 1 2
9 334337 A 0 1 0
9 390512 C 0 0 0
c <- c("9", "286593", "C", "0", "1", "2")
d <- c("9", "334337", "A", "0", " 1", "0")
e <- c("9", "390512", "C", "0", "0", "0")
dat <- data.frame(rbind(c,d,e))
I am interested to see the best way to do this as I want to get out of the habit of using nested For loops in R.