[R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Thu Jan 27 22:57:51 CET 2022


Javed,
You may misunderstand something here.
Forget ifelse() which does all kinds of things (which you can see by just typing "ifelse" and a carriage/return or ENTER.
Your initial goal should be kept in mind. You want to create a data structure, in this case a vector, that is the same length as another vector called test$operator in which you mark whether the corresponding element was exactly "T13" or not.
There is nothing fundamentally wrong with your approach albeit it is overkill in this case. As has been pointed out, SKIPPING ifelse() entirely, you can get a vector of Logicals (TRUE or FALSE) by a simple command like this:
    result <- test$operator == 'T13'
For many purposes, that is all you need. TRUE and FALSE are also sometimes mapped into 1 and 0 for various purposes, so you can convert them into integers or general numerics is that is needed. Consider the following code that checks the integers from 1 to 7 to see if they are even (as in divisible by 2):

> result <- 1:7 %% 2 == 0> result[1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE> as.integer(result)[1] 0 1 0 1 0 1 0> as.numeric(result)[1] 0 1 0 1 0 1 0> result <- as.integer(1:7 %% 2 == 0)> result[1] 0 1 0 1 0 1 0

If for some reason the choice of 1 and 0 is the opposite of what you need, you can invert them several ways with the simplest being:

    as.integer(1:7 %% 2 != 0)
or    as.integer(!(1:7 %% 2 != 0))

The first negates the comparison and the second just flips every FALSE and TRUE to the other.
Why are we talking about this? For many more interesting cases, ifelse() is great as you can replace one or both of the choices with anything. A very common case is replacing one choice with itself and changing the other, or nesting the comparisons in a sort of simulated tree as in 
    ifelse(some_condition,       ifelse(second_condition, result1, result2),         ifelse(third_condition, result3, result4)))

But you seem to want the simplest return of two values that also happen to be the underlying equivalent of TRUE and FALSE in many languages. In Python, anything that evaluates to zero (or the Boolean value FALSE) tends to be treated as FALSE, and anything else like a 1 or 666 is treated as TRUE, as shown below:

> if (TRUE) print("TRUE") else print("FALSE")[1] "TRUE"> if (1) print("TRUE") else print("FALSE")[1] "TRUE"> if (666) print("TRUE") else print("FALSE")[1] "TRUE"> if (FALSE) print("TRUE") else print("FALSE")[1] "FALSE"> if (0) print("TRUE") else print("FALSE")[1] "FALSE"

This is why you are being told that for many purposes, the Boolean vector may work fine. But if you really want or need zero and one, that is a trivial transformation as shown. Feel free to use ifelse() and then figure out what went wrong with your code, but also to try the simpler version and see if the problem goes away.
Avi
-----Original Message-----
From: javed khan <javedbtk111 using gmail.com>
To: Bert Gunter <bgunter.4567 using gmail.com>
Cc: R-help <r-help using r-project.org>
Sent: Thu, Jan 27, 2022 1:15 pm
Subject: Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed

Thank you Bert Gunter

Do you mean I should do something like this:

prot <- (as.numeric(ifelse(test$operator == 'T13', 1, 0))




On Thu, Jan 27, 2022 at 4:44 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> This will not help solve the issue, but perhaps it is worth pointing
> out that the idiom,
> prot <- ifelse(test$operator == 'T13', 1, 0)
> is perhaps more simply coded as
> prot <- test$operator == 'T13'
>
> This will give a logical, TRUE/FALSE, instead of 1/0, but I doubt that
> a numeric is needed anyway. However, as.numeric() would of course do
> such a conversion, in which case the original ifelse might as well be
> used.
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
> On Wed, Jan 26, 2022 at 11:39 PM PIKAL Petr <petr.pikal using precheza.cz>
> wrote:
> >
> > Hi
> >
> > Actually you did not. Your original question was:
> >
> > > Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
> > > I used this:
> > > var <- ifelse(test$operator == 'T14', 1, 0)
> > > operator has several values like T1, T3, T7, T15, T31, T37
> > > For some values like T3, T7 it works fine but for majority of values
> > > it gives error.
> > > When I use: is.na(ts$operator), it shows all false values so no NAs.
> >
> > Only now we could inspect your whole code and it was already pointed
> that the
> > error does not originate from ifelse.
> >
> > With the same data and ifelse code I did not get any error.
> >
> > test <- structure(list(DepthTree = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> > 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
> > <snip>
> >
> > > str(test)
> > 'data.frame':  146 obs. of  15 variables:
> >  $ DepthTree    : num  1 1 1 1 1 1 1 1 1 1 ...
> >  <snip>
> > $ numCovered  : num  0 0 0 0 0 0 0 0 0 0 ...
> >  $ operator    : Factor w/ 16 levels "T0","T1","T2",..: 4 4 7 8 8 8 11
> 4 10 7
> > ...
> >  $ methodReturn : Factor w/ 22 levels "I","V","Z","method",..: 2 2 2 2 2
> 2 2 4
> > 4 2 ...
> >  $ numTestsCover: num  16 15 15 16 15 15 15 4 4 16 ...
> >  $ mutantAssert : num  55 55 55 55 55 55 55 13 13 55 ...
> >  $ classAssert  : num  3 3 3 3 3 3 3 3 3 3 ...
> >  $ isKilled    : Factor w/ 2 levels "yes","no": 2 2 2 2 2 2 2 2 2 2 ...
> > >
> > prot <- ifelse(test$operator == 'T13', 1, 0)
> >
> > the most probable source of the error is
> >
> > fc= fairness_check(explainer,
> >                          protected = prot,
> >                    privileged = privileged)
> >
> > so you should check explainer and privileged
> >
> > Cheers
> > Petr






	[[alternative HTML version deleted]]



More information about the R-help mailing list