R中的条件

时间:2014-09-29 20:15:09

标签: r conditional

我不确定如何使这项工作?

我希望在数据框中创建一个新列,具体取决于其他两列是否符合以下条件:

如果df $ Cn_LCIS和df $ Cn_ILC都大于' 2'打印' 0'

如果df $ CN_LCIS和df $ Cn_ILC都小于' 2'打印' 0'

如果它们是相同的,也打印' 0'否则打印' 1'

DF

        Chromosome     Start       End Cn_ILC mCn_ILC Cn_LCIS mCn_LCIS both
chr1.1        chr1         0  11194349      2       1       2        1    0
chr1.2        chr1 102809740 104579163      2       1       3        1    1
chr1.3        chr1 104579163 121311799      2       1       2        1    0
chr1.4        chr1  11194349  11492125      2       1       3        1    1
chr1.5        chr1  11492125  71442329      2       1       2        1    0
chr1.6        chr1 144009053 157140292      1       1       1        1    0
chr1.7        chr1 157140292 243709339      2       1       1        1    0
chr1.8        chr1 243709339 244112662      3       1       3        1    0
chr1.9        chr1 244112662 249250621      3       1       3        1    0
chr1.10       chr1  71442329  72624878      2       1       3        1    1
chr1.11       chr1  72624878 102809740      2       1       4        1    1

不工作:

df$both <- ifelse(df$Cn_LCIS > 2 & df$Cn_ILC > 2, 0, ifelse (df$Cn_LCIS < 2 & df$Cn_ILC < 2, 0, ifelse (df$Cn_LCIS == 2 & df$Cn_LCIS == 2, 0, ifelse(df$Cn_ILC!=df$Cn_LCIS,1))))

5 个答案:

答案 0 :(得分:2)

compareTo2 <- function(LCIS, ILC) {
  as.numeric(!((LCIS > 2 & ILC > 2) | (LCIS < 2 & ILC < 2) | (LCIS == ILC) )) 
}

compareTo2(df$Cn_LCIS, df$Cn_ILC)
 # [1] 0 1 0 1 0 0 1 0 0 1 1

如果您的数据很大,您可能需要尝试以下

library(data.table)

DT <- as.data.table(df)

## different syntax for data.table than data.frame
DT[, as.numeric(!((Cn_LCIS > 2 & Cn_ILC > 2) | (Cn_LCIS < 2 & Cn_ILC < 2) | (Cn_LCIS == Cn_ILC) )) ]

## ... or you can assign it as a column
DT[, compare_LCIS_ILC := as.numeric(!((Cn_LCIS > 2 & Cn_ILC > 2) | (Cn_LCIS < 2 & Cn_ILC < 2) | (Cn_LCIS == Cn_ILC) )) ]
DT
#         Chromosome     Start       End Cn_ILC mCn_ILC Cn_LCIS mCn_LCIS both compare_LCIS_ILC
#  1:       chr1         0  11194349      2       1       2        1    0                0
#  2:       chr1 102809740 104579163      2       1       3        1    1                1
#  3:       chr1 104579163 121311799      2       1       2        1    0                0
#  4:       chr1  11194349  11492125      2       1       3        1    1                1
#  5:       chr1  11492125  71442329      2       1       2        1    0                0
#  6:       chr1 144009053 157140292      1       1       1        1    0                0
#  7:       chr1 157140292 243709339      2       1       1        1    0                1
#  8:       chr1 243709339 244112662      3       1       3        1    0                0
#  9:       chr1 244112662 249250621      3       1       3        1    0                0
# 10:       chr1  71442329  72624878      2       1       3        1    1                1
# 11:       chr1  72624878 102809740      2       1       4        1    1                1

答案 1 :(得分:2)

transform(df, both = ifelse((Cn_LCIS > 2 & Cn_ILC > 2) | 
                             (Cn_LCIS < 2 & Cn_ILC < 2) |
                              (Cn_LCIS == 2 & Cn_LCIS == 2), 0, 1))

#         Chromosome     Start       End Cn_ILC mCn_ILC Cn_LCIS mCn_LCIS both
# chr1.1        chr1         0  11194349      2       1       2        1    0
# chr1.2        chr1 102809740 104579163      2       1       3        1    1
# chr1.3        chr1 104579163 121311799      2       1       2        1    0
# chr1.4        chr1  11194349  11492125      2       1       3        1    1
# chr1.5        chr1  11492125  71442329      2       1       2        1    0
# chr1.6        chr1 144009053 157140292      1       1       1        1    0
# chr1.7        chr1 157140292 243709339      2       1       1        1    1
# chr1.8        chr1 243709339 244112662      3       1       3        1    0
# chr1.9        chr1 244112662 249250621      3       1       3        1    0
# chr1.10       chr1  71442329  72624878      2       1       3        1    1
# chr1.11       chr1  72624878 102809740      2       1       4        1    1

答案 2 :(得分:1)

您的代码存在的问题是,您的最内层ifelse没有&#34;否则&#34;论点。如果您只是在最里面的, 0中添加ifelse,您的代码就会运行。 Thad说,其他一些答案更清晰。

答案 3 :(得分:1)

另一种可能性是使用withwithin

with(df, {
    cb <- cbind(Cn_LCIS, Cn_ILC)
    aa <- (cb > 2) | (cb < 2) | (cb[1] == cb[2])
    (!(aa[,1] == aa[,2]))+0
})
# [1] 0 1 0 1 0 0 1 0 0 1 1

答案 4 :(得分:1)

更简洁:

with(df, sign((Cn_LCIS-2)*(Cn_ILC-2))-(Cn_ILC != Cn_LCIS) < 0)*1
# [1] 0 1 0 1 0 0 1 0 0 1 1