否则如果声明

时间:2013-10-28 22:22:18

标签: r if-statement

我在R中运行以下内容:

if (mito$count > 1) {
mito$Polymorphism  <- "SNP"
} else if ((mito$count == 1) & (mito$A_GT == mito$ref | mito$C_GT == mito$ref | mito$G_GT == mito$ref | mito$T_GT == mito$ref)) {
mito$Polymorphism  <- "No"
} else {
    mito$Polymorphism <- ""
}

它给了我每个人似乎得到的通常错误:

1: In if (mito$count > 1) { :
the condition has length > 1 and only the first element will be used
2: In if ((mito$count == 1) & (mito$A_GT == mito$ref | mito$C_GT ==  :
the condition has length > 1 and only the first element will be used

我认为这与单值和矢量(前者,我想做)有关。我需要事先指定数据框吗?

1 个答案:

答案 0 :(得分:2)

ifelse()在这种情况下工作:

mito$Polymorphism <- ifelse(mito$count > 1, 
                            'SNP', 
                            ifelse(((mito$count == 1) & 
                                    (mito$A_GT == mito$ref |
                                     mito$C_GT == mito$ref |
                                     mito$G_GT == mito$ref |
                                     mito$T_GT == mito$ref)),
                                   'NO',
                                   ''))