Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
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 To: Bert Gunter Cc: R-help 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)) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
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 To: Bert Gunter Cc: R-help 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)) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
Timothy, In reply to what you wrote about a benchmark suggesting some storage formats may make the code run slower, it is not a surprise, given what you chose to benchmark. You are using a test of a logical variable in a numeric context when you have code like: log(a2+0.01) In order to do the calculation, depending on the internals, you need to convert a2 to at least an integer or perhaps a floating point value such as 1L or 1.0 before adding 0.01 to it. You are doing the equivalent of: log(as.integer(a2)+0.01) or perhaps: log(as.double(a2)+0.01) The result is some extra work in THAT context. Note I am NOT saying R calls one of those primitive functions, just that the final code does such conversions perhaps at the assembler level or lower. But consider the opposite context such as in a if(...) statement as in: if(a2) {do_this) else {do_that} If a2 is already a logical data form, it happens rapidly. If a2 is something more complex that can be evaluated in steps into a logical, it takes those steps. As I showed earlier, if a2 was 1 or 666 it would be evaluated to be non-zero and thus converted to TRUE and then the statement would choose do_this, else it would evaluate to FALSE and do do_that. So the right storage format depends on how you want to use it and how much storage space you are willing to use. On some machines and architectures, they may store a logical value in anything from a bit to a byte to multiple bytes, and on a lower level, it may be expanded as needed to fit into a fixed register on the CPU. In some cases, a natural storage format will be the one that can be used immediately with no boxing or unboxing. But as always, there are tradeoffs to be considered in terms of how many cycles are used (execution time) or other resources like memory in use. In a few cases, it may oddly pay to make two or more copies of a vector in different storage formats and then use the best one for subsequent calculations. Boolean might turn out to be a great choice for indexing into a list or vector or matrix or data.frame, while integer may be great if doing mathematics like multiplication into a structure that only contains integers, and a double version when interacting with such numbers and maybe even versions that are character or complex. But from a novice perspective, performance is not usually a big concern and especially not for small amounts of data. The only reason this is being discussed is that the question about what went wrong might be hard to figure out without lots more info, while the simple wrok-around might either work fine or tell us more about what might be wrong. -Original Message- From: Ebert,Timothy Aaron To: Bert Gunter Cc: R-help Sent: Thu, Jan 27, 2022 2:27 pm Subject: Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed You did not claim it is faster, but if one is writing large programs or has huge quantities of data then thinking about execution time could be useful. if(!require(microbenchmark)){install.packages("microbenchmark")} library(microbenchmark) a1<-c(1,1,0,1,0,0,0,1,1,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,1,0,1,0,1,0,0,0,1,1,0,0,1,0,0,0,0,1,1,0,0,1) a2=as.logical(a1) microbenchmark( { log(a1+0.01) }, { log(a2+0.01) }, times=10 ) On my system running the code shows that there is an overhead cost if the logical has to be converted. In this simple code it was a sometimes significant but always a trivial 0.1 microsecond cost. I tried a few other bits of code and the mean and minimum values were always smaller performing numeric operations on a numeric variable. However, it looks like the range in values for a numeric operation on a numeric variable is greater. I don't understand why. Tim -Original Message- From: Bert Gunter Sent: Thursday, January 27, 2022 1:17 PM To: Ebert,Timothy Aaron Cc: PIKAL Petr ; R-help Subject: Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed [External Email] I did not claim it is faster -- and in fact I doubt that it makes any real difference. Just simpler, imo. I also think that the logical vector would serve equally in any situation in most cases where arithmetic 0/1 coding is used -- even arithmetic ops and comparisons: > TRUE + 2 [1] 3 > TRUE > .5 [1] TRUE (?'+' has details) I would appreciate someone responding with a nontrivial counterexample to this claim if they have one, other than the sort of thing shown in the ?logical example involving conversion to character: ## logical interpretation of particular strings charvec <- c("FALSE", "F", "False", "false", "fAlse", "0", "TRUE", "T", "True", "true", "tRue", "1") as.logical(charvec) ## factors are converted via their levels, so string conversion is used as.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
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, > str(test) 'data.frame': 146 obs. of 15 variables: $ DepthTree: num 1 1 1 1 1 1 1 1 1 1 ... $ 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 From: javed khan Sent: Wednesday, January 26, 2022 3:45 PM To: PIKAL Petr Cc: R-help Subject: Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed Hi Pikal, why would I hide something? I provided just a code where error is. Full code is: index= sample(1:nrow(data), 0.7*nrow(data)) train= data[index,] test= data[-index,] task = TaskClassif$new("data", backend = train, target = "isKilled") learner= lrn("classif.gbm", predict_type = "prob") model= learner$train(task ) explainer = explain_mlr3(model, data = test[,-15], y = as.numeric(test$isKilled)-1, label="GBM") prot <- ifelse(test$operator == 'T13', 1, 0) privileged <- '1' fc= fairness_check(explainer, protected = prot, privileged = privileged) plot(fc) And my data is the following: str(test) 'data.frame': 146 obs. of 15 variables: $ DepthTree: num 1 1 1 1 1 1 1 1 1 1 ... $ NumSubclass : num 0 0 0 0 0 0 0 0 0 0 ... $ McCabe : num 1 3 3 3 3 3 3 1 1 2 ... $ LOC : num 3 10 10 10 10 10 10 4 4 5 ... $ DepthNested : num 1 2 2 2 2 2 2 1 1 2 ... $ CA : num 1 1 1 1 1 1 1 1 1 1 ... $ CE : num 2 2 2 2 2 2 2 2 2 2 ... $ Instability : num 0.667 0.667 0.667 0.667 0.667 0.667 0.667 0.667 0.667 0.667 ... $ 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 ... > dput(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, 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, 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, 2, 2, 1, 2, 1, 1, 1), NumSubclass = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2), McCabe = c(1, 3, 3, 3, 3, 3, 3, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1, 2, 2, 2, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 2, 2, 5, 5, 5, 5, 5, 5, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1,
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
Your error and your code don't match. Please spend some time to make a small reproducible example [1][2] when posting a question... you may even figure out your own answer before you send it out. [1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example [2] http://adv-r.had.co.nz/Reproducibility.html On January 26, 2022 5:47:16 AM PST, javed khan wrote: >I get this error: > >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. > >Where could be the problem? > > [[alternative HTML version deleted]] > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
Hi It seems that you are hiding what you really do. This > options(error = NULL) works fine without any error. So please If you want some reasonable answer post question with data and code which is causing the error. My wild guess is that you have some objects in your environment and you do not know that they are used in you commands. Try to start fresh R session and try to inspect your environment with ls() Cheers Petr > -Original Message- > From: R-help On Behalf Of javed khan > Sent: Wednesday, January 26, 2022 3:05 PM > To: Ivan Krylov > Cc: R-help > Subject: Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE > needed > > Ivan, thanks > > When I use options(error = NULL) > > it says: Error during wrapup: missing value where TRUE/FALSE needed > Error: no more error handlers available (recursive errors?); invoking 'abort' > restart > > With traceback(), I get > > 4: readable_number(max_value - min_value, FALSE) > 3: get_nice_ticks(lower_bound, upper_bound) > > On Wed, Jan 26, 2022 at 2:53 PM Ivan Krylov wrote: > > > On Wed, 26 Jan 2022 14:47:16 +0100 > > javed khan wrote: > > > > > Error in if (fraction <= 1) { : missing value where TRUE/FALSE > > > needed > > > > > var <- ifelse(test$operator == 'T14', 1, 0) > > > > The error must be in a place different from your test$operator > > comparison. Have you tried traceback() to get the call stack leading > > to the error? Or options(error = recover) to land in a debugger > > session the moment an uncaught error happens? (Use options(error = > > NULL) to go back to the default behaviour.) > > > > Unrelated: var <- test$operator == 'T14' will also give you an > > equivalent logical vector with a bit less work. > > > > -- > > Best regards, > > Ivan > > > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
Hi Do not post in HTML, please. Try to show your real data - use str(test), or preferably dput(test). If test is big, use only fraction of it The problem must be probably in your data. x <- sample(1:20, 100, replace=T) fake <- paste("T", x, sep="") ifelse(fake=="T14", 1,0) [1] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 head(fake) [1] "T7" "T9" "T3" "T9" "T12" "T9" > str(fake) chr [1:100] "T7" "T9" "T3" "T9" "T12" "T9" "T19" "T19" "T12" "T2" "T17" ... > Cheers Petr > -Original Message- > From: R-help On Behalf Of javed khan > Sent: Wednesday, January 26, 2022 2:47 PM > To: R-help > Subject: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE > needed > > I get this error: > > 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. > > Where could be the problem? > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
On Wed, 26 Jan 2022 14:47:16 +0100 javed khan wrote: > Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed > var <- ifelse(test$operator == 'T14', 1, 0) The error must be in a place different from your test$operator comparison. Have you tried traceback() to get the call stack leading to the error? Or options(error = recover) to land in a debugger session the moment an uncaught error happens? (Use options(error = NULL) to go back to the default behaviour.) Unrelated: var <- test$operator == 'T14' will also give you an equivalent logical vector with a bit less work. -- Best regards, Ivan __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Error in if (fraction <= 1) { : missing value where TRUE/FALSE needed
I get this error: 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. Where could be the problem? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.