2016-02-26 54 views
3

我有以下矩陣:應用的if-else功能逐行

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"))) 

,看起來像這樣:

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" 

我怎麼會看每行中,取而代之的0的出現列allele1中的值,以及列allele2中的值出現1?

回答

4

你可以做直邏輯更換。它需要幾行代碼,但可以正常工作。這個想法是複製我們正在替換的列數的第一列和第二列。我們可以用例如m[, c(1, 1, 1, 1)],複製第一列四次。這不使用循環。

## 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] 

導致修改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" 
+0

選項2與我的邏輯類似,並且我猜可能會更快。 – thelatemail

+0

你很有趣。花了我一分鐘才弄清楚。但它絕對不會做我在這裏做的所有重複。我猜他們即使在速度上也是如此。 –

+0

對不起理查德,選項1似乎沒有按預期工作(試用它並將輸出與選項2的輸出進行比較)。這幾天我一直在竊聽我。選項2完美地工作(這是我用過的)。 – PeterQ

4

一個易於閱讀的選項將使用apply()函數。在下面的代碼,我認爲你的樣品基質被存儲在一個名爲x變量:

# replace "0" with allele1 
x <- apply(x, 1, function(x) { 
        x[x == "0"] <- x[1] 
        return(x) 
       }) 

# replace "1" with allele2 
x <- apply(x, 1, function(x) { 
        x[x == "1"] <- x[2] 
        return(x) 
       }) 
+0

這是不以任何方式有用的匿名downvote工作回答。 –

+0

謝謝蒂姆,我起初的做法與你一樣,但想要更像理查德那樣神奇的東西。 – PeterQ

+1

理查德的解決方案當然是神奇的,但我會留下這張貼,以防有人想要一個更易於閱讀的答案。 –

4

打破它塊,避免行按行操作:

df <- as.data.frame(df,stringsAsFactors=FALSE) 
df[-(1:2)] <- lapply(df[-(1:2)], as.numeric) 

zerosel <- which(df[-(1:2)]==0, arr.ind=TRUE) 
onesel <- which(df[-(1:2)]==1, arr.ind=TRUE) 

df[-(1:2)][zerosel] <- df$allele1[zerosel[,1]] 
df[-(1:2)][onesel] <- df$allele2[onesel[,1]] 


# allele1 allele2 s1a s1b s2a s2b s3a s3b s4a s4b s5a s5b s6a s6b ... 
#1  G  A G G G G G G G G G G G A ... 
#2  G  A G G G G G G G G G G G G ... 
#3  A  G G A G A A G G A A G A G ... 
#4  C  A C C C C A C C C C C C C ... 
#5  G  A G G G G G G G G G G G A ... 
#6  G  A G G G G G G G G G G G G ...