lundi 11 février 2019

How to use the ifelse function for two conditions combining strings and values?

I know there's a lot of questions on the ifelse function, but I can't seem to figure out how to make my code to work. I've been inspired by solutions posted in previous questions but I'm still having error messages. Below is a sample of my data:

     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

In this case, I'd like to create a 5th column results2 with ifelse having two conditions:

1) If probability is greater than 0.5 and results1 is "POSITIVE", I would like results2 to be "TRUE". If probability is lower than 0.5 and results1 is "POSITIVE", I would like results2 to be "FALSE".

2) If probability is greater than 0.5 and results1 is "NEGATIVE", I would like results2 to be "FALSE". If probability is lower than 0.5 and results1 is "NEGATIVE", I would like results2 to be "TRUE".

I've been using different syntaxes for this, such as:

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

I'm getting the error: Error in ifelse(collar$results1 == "POSITIVE", collar$probability > 0.5, : unused argument ("FALSE")

Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire