r - Apply if-else function row-wise -


i have 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 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 in each row , replace occurrences of 0 value in column allele1, , occurrences of 1 value in column allele2?

you straight logical replacement. takes few lines of code, work fine. idea replicate first , second columns number of columns replacing. can e.g. m[, c(1, 1, 1, 1)], replicating first column 4 times. uses no loops.

## find zeros is0 <- m[, -(1:2)] == 0 ## replace values replicating relevant columns ## applying 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 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"  

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -