逐行应用if-else函数

时间:2016-02-26 01:38:12

标签: r

我有以下矩阵:

<li>

看起来像这样:

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中的值出现次数为1?

3 个答案:

答案 0 :(得分: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)
                 })

答案 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" 

答案 2 :(得分: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 ...