如何在两个条件下使用ifelse函数将字符串和值组合在一起?

时间:2019-02-11 11:31:50

标签: r if-statement

我知道ifelse函数有很多问题,但是我似乎无法弄清楚如何使代码正常工作。我受到以前问题中发布的解决方案的启发,但仍然收到错误消息。以下是我的数据示例:

     observed predicted probability results1
1     Head-up   Grazing   0.2727273 NEGATIVE
2     Head-up   Grazing   0.7272727 NEGATIVE
3     Head-up   Grazing   0.7272727 NEGATIVE
4     Head-up   Grazing   0.5454545 NEGATIVE
5     Head-up   Grazing   0.7272727 NEGATIVE
6     Head-up   Grazing   0.4545455 NEGATIVE
7     Head-up Vigilance   0.3636364 NEGATIVE
8     Head-up   Grazing   0.3636364 NEGATIVE
9     Head-up Vigilance   0.3636364 NEGATIVE
10    Unknown   Grazing   0.3636364 NEGATIVE
11     Moving   Head-up   0.4545455 NEGATIVE
12     Moving   Grazing   0.3636364 NEGATIVE
13    Head-up   Grazing   0.4545455 NEGATIVE
14    Head-up   Grazing   0.3636364 NEGATIVE
15    Head-up   Grazing   0.4545455 NEGATIVE
16    Head-up   Grazing   0.3636364 NEGATIVE
17    Head-up   Head-up   0.4545455 POSITIVE

在这种情况下,我想创建第五列results2,其中ifelse具有两个条件:

1)如果probability大于0.5并且results1"POSITIVE",我希望results2"TRUE"。如果probability小于0.5并且results1"POSITIVE",我希望results2"FALSE"

2)如果probability大于0.5并且results1"NEGATIVE",我希望results2"FALSE"。如果probability小于0.5并且results1"NEGATIVE",我希望results2"TRUE"

为此,我一直使用不同的语法,例如:

collar$result2<-ifelse(collar$results1=="POSITIVE" & collar$probability>0.5,"TRUE","FALSE"|ifelse(collar$results1=="NEGATIVE" & collar$probability>0.5,"FALSE","TRUE"))

我收到错误消息:Error in ifelse(collar$results1 == "POSITIVE", collar$probability > 0.5, : unused argument ("FALSE")

感谢您的帮助!

P.S。如果使用项圈:$results2<-dplyr::mutate(collar,results2 = case_when( (probability > 0.5 & results1 == "POSITIVE") | (probability < 0.5 & results1 == "NEGATIVE") ~ TRUE, TRUE ~ FALSE) )我得到(样本):

> print(collar)
     observed predicted probability results1 results2.observed results2.predicted results2.probability results2.results1
1     Head-up Vigilance   0.2727273 NEGATIVE           Head-up          Vigilance            0.2727273          NEGATIVE
2     Head-up   Grazing   0.7272727 NEGATIVE           Head-up            Grazing            0.7272727          NEGATIVE
3     Head-up   Grazing   0.7272727 NEGATIVE           Head-up            Grazing            0.7272727          NEGATIVE
4     Head-up   Grazing   0.5454545 NEGATIVE           Head-up            Grazing            0.5454545          NEGATIVE
5     Head-up   Grazing   0.7272727 NEGATIVE           Head-up            Grazing            0.7272727          NEGATIVE
6     Head-up   Grazing   0.4545455 NEGATIVE           Head-up            Grazing            0.4545455          NEGATIVE

1 个答案:

答案 0 :(得分:0)

编写自己的函数可能比将其嵌套在ifelse()函数中更好。以下内容似乎符合您的要求(请注意,您可能遇到概率== 0.5的情况;此处将返回NA):

results2 <- apply(collar,1,function(x)
  {
    if (x["results1"]=="POSITIVE" & x["probability"]>0.5) TRUE
    else if (x["results1"]=="POSITIVE" & x["probability"] < 0.5) FALSE
    else if (x["results1"]=="NEGATIVE" & x["probability"]>0.5) FALSE
    else if (x["results1"]=="NEGATIVE" & x["probability"]<0.5) TRUE
    else NA
  })

collar$results2 <- results2