I have the below matrix:
structure(c("G", "G", "A", "C", "G", "G", "A", "A", "G", "A",
"A", "A", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "0", "0",
"0", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "0",
"0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "0", "0", "0",
"0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0",
"1", "0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0",
"0", "0", "0", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0",
"1", "1", "0", "0", "0"), .Dim = c(6L, 22L), .Dimnames = list(
c("1", "2", "3", "4", "5", "6"), c("allele1", "allele2",
"s1a", "s1b", "s2a", "s2b", "s3a", "s3b", "s4a", "s4b", "s5a",
"s5b", "s6a", "s6b", "s7a", "s7b", "s8a", "s8b", "s9a", "s9b",
"s10a", "s10b")))
Which looks like this:
allele1 allele2 s1a s1b s2a s2b s3a s3b s4a s4b s5a s5b s6a s6b s7a s7b s8a s8b s9a s9b s10a s10b
1 "G" "A" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0" "0"
2 "G" "A" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "1"
3 "A" "G" "1" "0" "1" "0" "0" "1" "1" "0" "0" "1" "0" "1" "0" "0" "1" "1" "1" "0" "1" "1"
4 "C" "A" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "1" "0" "0"
5 "G" "A" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "1" "0" "0" "0"
6 "G" "A" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "0" "0" "0" "0" "0"
How could I look in each row and replace the occurrences of 0 with the value in column allele1
, and the occurrences of 1 with the value in column allele2
?
You could do straight logical replacement. It takes a few lines of code, but would work fine. The idea is to replicate the first and second columns for the number of columns we are replacing. We can do that with e.g. m[, c(1, 1, 1, 1)]
, replicating the first column four times. This uses no loops.
## find the zeros
is0 <- m[, -(1:2)] == 0
## replace the values by replicating the relevant columns
## then applying the logical subset
m[, -(1:2)][is0] <- m[, rep(1, ncol(m)-2)][is0]
m[, -(1:2)][!is0] <- m[, rep(2, ncol(m)-2)][!is0]
resulting in the modified m
m
# allele1 allele2 s1a s1b s2a s2b s3a s3b s4a s4b s5a s5b s6a s6b s7a s7b s8a s8b s9a s9b s10a s10b
# 1 "G" "A" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G" "A" "G" "G" "G"
# 2 "G" "A" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "A"
# 3 "A" "G" "G" "A" "G" "A" "A" "G" "G" "A" "A" "G" "A" "G" "A" "A" "G" "G" "G" "A" "G" "G"
# 4 "C" "A" "C" "C" "C" "C" "A" "C" "C" "C" "C" "C" "C" "C" "C" "A" "C" "C" "C" "A" "C" "C"
# 5 "G" "A" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G" "A" "G" "G" "G"
# 6 "G" "A" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "G" "A" "G" "G" "G" "G" "G"
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments