Ebert,Timothy Aaron
2022-Jan-27 19:27 UTC
[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=100000
)
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 <bgunter.4567 at gmail.com>
Sent: Thursday, January 27, 2022 1:17 PM
To: Ebert,Timothy Aaron <tebert at ufl.edu>
Cc: PIKAL Petr <petr.pikal at precheza.cz>; R-help <r-help at
r-project.org>
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.logical(factor(charvec))
as.logical(factor(c(0,1))) # "0" and "1" give NA
(I mean of course purely internal R code, not export of data to an external
application).
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
Thu, Jan 27, 2022 at 8:12 AM Ebert,Timothy Aaron <tebert at ufl.edu>
wrote:>
> One could use the microbenchmark package to compare which approach is
faster, assuming the dataset is large enough that the outcome will make a
measurable difference.
>
> -----Original Message-----
> From: R-help <r-help-bounces at r-project.org> On Behalf Of Bert
Gunter
> Sent: Thursday, January 27, 2022 10:44 AM
> To: PIKAL Petr <petr.pikal at precheza.cz>
> Cc: R-help <r-help at r-project.org>
> Subject: Re: [R] Error in if (fraction <= 1) { : missing value where
> TRUE/FALSE needed
>
> [External Email]
>
> 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 at
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
> >
> > From: javed khan <javedbtk111 at gmail.com>
> > Sent: Wednesday, January 26, 2022 3:45 PM
> > To: PIKAL Petr <petr.pikal at precheza.cz>
> > Cc: R-help <r-help at r-project.org>
> > 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,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> = c(3, 10, 10, 10, 10, 10, 10, 4, 4, 5, 22, 22, 22, 22, 22, 22, 22,
> > 22, 3, 3, 3, 3, 3, 8, 8, 8, 4, 23, 23, 23, 23, 23, 23, 23, 23, 23,
> > 23, 16, 16, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 16, 16, 16, 16, 16,
> > 16, 20, 20, 20, 20, 20, 20, 20, 20, 7, 7, 7, 7, 18, 18, 18, 18, 18,
> > 18, 3, 15, 15, 15, 15, 15, 15, 15, 15, 15, 6, 15, 15, 15, 15, 15,
> > 15, 15, 15, 15, 9, 9, 9, 9, 9, 9, 4, 4, 3, 3, 3, 3, 3, 4, 4, 6, 6,
> > 5, 5, 5, 8, 8, 8, 3, 3, 3, 3, 3, 3, 7, 3, 3, 3, 3, 15, 15, 15, 15,
> > 15, 15, 15, 15, 3, 3, 3, 3, 4, 4, 8, 4, 4, 4, 3), DepthNested = c(1,
> > 2, 2, 2, 2, 2, 2, 1, 1, 2, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 2,
> > 2, 2, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
> > 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
> > 3, 3, 3, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
> > 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1,
> > 1, 1, 2, 1, 1, 1, 1), CA = 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, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 1), CE = c(2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
> > 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2,
> > 2, 0, 2, 0, 0, 2), Instability = c(0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 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.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667,
> > 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0, 0,
> > 0, 0, 0, 0, 0, 0, 0, 0, 0.667, 0.667, 0.667, 0.667, 0.667, 0.667, 0,
> > 0.667, 0, 0, 0.667), numCovered = 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, 150,
> > 54, 54, 123, 54, 54, 123, 54, 189, 189, 189, 138, 33, 18, 27, 27,
> > 18, 27, 15, 54, 54, 150, 150, 54, 150, 150, 54, 150, 66, 60, 12, 60,
> > 15, 45, 72, 12, 12, 45, 255, 255, 255, 255, 129, 129, 0, 0, 6, 6, 6,
> > 6, 303, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15,
> > 21, 12, 12, 12, 12, 12, 12, 12, 12, 48, 1557, 9, 12, 0, 0, 141, 0,
> > 21, 21, 21), operator = structure(c(4L, 4L, 7L, 8L, 8L, 8L, 11L, 4L,
> > 10L, 7L, 4L, 7L, 8L, 8L, 8L, 9L, 9L, 11L, 7L, 8L, 2L, 8L, 8L, 7L,
> > 8L, 10L, 11L, 1L, 2L, 2L, 4L, 8L, 8L, 8L, 8L, 10L, 12L, 4L, 6L, 6L,
> > 8L, 8L, 8L, 7L, 7L, 8L, 8L, 9L, 9L, 1L, 4L, 6L, 8L, 8L, 8L, 1L, 1L,
> > 7L, 7L, 7L, 8L, 8L, 13L, 7L, 8L, 9L, 9L, 1L, 1L, 2L, 7L, 8L, 10L,
> > 10L, 4L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 10L, 1L, 8L, 8L, 8L, 8L,
> > 10L, 11L, 11L, 11L, 6L, 7L, 8L, 9L, 9L, 10L, 6L, 10L, 1L, 4L, 7L,
> > 10L, 6L, 7L, 13L, 7L, 13L, 7L, 8L, 9L, 4L, 6L, 9L, 8L, 10L, 8L, 10L,
> > 8L, 10L, 9L, 8L, 10L, 13L, 10L, 2L, 7L, 7L, 8L, 8L, 9L, 11L, 11L,
> > 10L, 10L, 13L, 13L, 8L, 8L, 9L, 4L, 6L, 10L, 10L), .Label =
c("T0",
> > "T1", "T2", "T3", "T4",
"T5", "T6", "T7", "T8", "T9",
"T10", "T11",
> > "T12", "T13", "T14", "T15"),
class = "factor"), methodReturn =
> > structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 2L, 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> > 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> > 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 1L, 1L, 1L, 1L, 3L, 3L,
> > 1L, 1L, 3L, 3L, 3L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 4L,
> > 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
> > 4L, 4L, 3L, 3L, 2L, 2L, 4L, 4L, 1L, 3L, 1L, 1L, 4L), .Label =
c("I",
> > "V", "Z", "method", "D",
"[D", "[[D", "J", "[I", "C",
"[J", "[C",
> > "[S", "F", "[F", "[B",
"S", "B", "[Z", "[[S", "[[B",
"[[Z"), class =
> > "factor"), numTestsCover = c(16, 15, 15, 16, 15, 15, 15, 4,
4, 16,
> > 16, 16, 15, 15, 15, 16, 16, 15, 3, 3, 2, 2, 2, 16, 11, 16, 4, 16,
> > 16, 3, 16, 16, 4, 16, 16, 16, 16, 2, 3, 2, 1, 4, 1, 16, 15, 16, 15,
> > 16, 16, 2, 2, 2, 3, 2, 2, 4, 5, 5, 4, 5, 5, 4, 5, 4, 4, 4, 4, 4, 3,
> > 4, 4, 3, 4, 4, 5, 5, 4, 4, 5, 4, 4, 5, 4, 4, 4, 3, 4, 4, 4, 4, 3, 3,
> > 4, 4, 4, 4, 4, 6, 6, 0, 0, 2, 2, 2, 2, 7, 17, 17, 15, 15, 15, 15,
> > 15, 16, 16, 16, 16, 15, 16, 16, 15, 17, 17, 16, 16, 5, 4, 4, 4, 4,
> > 4, 4, 4, 4, 4, 16, 16, 3, 4, 0, 0, 5, 0, 4, 4, 1), mutantAssert =
> > c(55, 55, 55, 55, 55, 55, 55, 13, 13, 55, 55, 55, 55, 55, 55, 55,
> > 55, 55, 9, 9, 9, 9, 9, 55, 41, 55, 13, 55, 55, 5, 55, 55, 13, 55,
> > 55, 55, 55, 8, 13, 8, 4, 13, 4, 55, 55, 55, 55, 55, 55, 9, 9, 9, 9,
> > 9, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 6, 9, 9, 6, 9, 9, 9, 9,
> > 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1L, 1L,
> > 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L,
> > 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,
> > 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
> > 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 2L,
> > 1L, 2L, 1L, 1L, 1L), .Label = c("yes", "no"),
class =
> > "factor")), row.names = c(3L, 9L, 12L, 13L, 14L, 15L, 19L,
21L, 22L,
> > 24L, 28L, 29L, 39L, 44L, 46L, 51L, 52L, 57L, 62L, 63L, 68L, 71L,
> > 73L, 75L, 77L, 80L, 83L, 84L, 85L, 86L, 90L, 95L, 100L, 102L, 103L,
> > 108L, 112L, 120L, 122L, 123L, 142L, 143L, 146L, 158L, 159L, 160L,
> > 163L, 167L, 168L, 170L, 174L, 176L, 183L, 186L, 192L, 196L, 199L,
> > 203L, 205L, 206L, 208L, 214L, 221L, 226L, 229L, 233L, 234L, 235L,
> > 236L, 237L, 238L, 249L, 252L, 255L, 260L, 267L, 270L, 271L, 273L,
> > 275L, 276L, 277L, 278L, 286L, 288L, 293L, 295L, 297L, 298L, 300L,
> > 301L, 302L, 306L, 312L, 313L, 316L, 317L, 331L, 333L, 338L, 341L,
> > 342L, 344L, 346L, 348L, 359L, 370L, 375L, 378L, 381L, 382L, 383L,
> > 386L, 389L, 390L, 394L, 397L, 400L, 402L, 403L, 404L, 408L, 415L,
> > 418L, 419L, 423L, 425L, 426L, 429L, 431L, 432L, 433L, 444L, 446L,
> > 447L, 448L, 449L, 451L, 452L, 457L, 458L, 470L, 474L, 476L, 479L,
> > 484L), class > > "data.frame")
> >
> >
> >
> >
> > On Wed, Jan 26, 2022 at 3:29 PM PIKAL Petr
> > <mailto:petr.pikal at precheza.cz>
> > wrote:
> > 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 <mailto:r-help-bounces at r-project.org> On
Behalf Of
> > > javed khan
> > > Sent: Wednesday, January 26, 2022 3:05 PM
> > > To: Ivan Krylov <mailto:krylov.r00t at gmail.com>
> > > Cc: R-help <mailto:r-help at r-project.org>
> > > 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
> > > <mailto:krylov.r00t at gmail.com>
> > > wrote:
> > >
> > > > On Wed, 26 Jan 2022 14:47:16 +0100 javed khan
> > > > <mailto:javedbtk111 at gmail.com> 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]]
> > >
> > > ______________________________________________
> > > mailto:R-help at r-project.org mailing list -- To UNSUBSCRIBE and
> > > more, see
> > >
https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_
> > > ma
> > >
ilman_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh
> > > 2k
> > >
VeAsRzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ
> > > 1-
qp1QpqM4aMJUU&s=nKOUnbWqTVHBJ8_18UPI_0txX86s35CP6VAqBc-Q6_k&e> >
> PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.
> > or
> >
g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kV
> > eA
> >
sRzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ1-qp1
> > Qp
qM4aMJUU&s=eZti_glRZlkKsUposffe3dMBirNV2J-goX3ARZowxLs&e> > >
and provide commented, minimal, self-contained, reproducible code.
> > ______________________________________________
> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_ma
> > il
> >
man_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVe
> > As
> >
Rzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ1-qp1Q
> > pq M4aMJUU&s=nKOUnbWqTVHBJ8_18UPI_0txX86s35CP6VAqBc-Q6_k&e>
> PLEASE do read the posting guide
> > https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.
> > or
> >
g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kV
> > eA
> >
sRzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ1-qp1
> > Qp
qM4aMJUU&s=eZti_glRZlkKsUposffe3dMBirNV2J-goX3ARZowxLs&e> > and
provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://urldefense.proofpoint.com/v2/url?u=https-3A__stat.ethz.ch_mail
>
man_listinfo_r-2Dhelp&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeAs
> Rzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ1-qp1Qpq
> M4aMJUU&s=nKOUnbWqTVHBJ8_18UPI_0txX86s35CP6VAqBc-Q6_k&e> PLEASE
do read the posting guide
> https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.or
>
g_posting-2Dguide.html&d=DwICAg&c=sJ6xIWYx-zLMB3EPkvcnVg&r=9PEhQh2kVeA
> sRzsn7AkP-g&m=eLthULmcsbsauwFtFwUELWbEcCm6j9dDsFTJTMNM9azTjEAhJ1-qp1Qp
> qM4aMJUU&s=eZti_glRZlkKsUposffe3dMBirNV2J-goX3ARZowxLs&e> and
provide commented, minimal, self-contained, reproducible code.
Avi Gross
2022-Jan-27 22:18 UTC
[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 <tebert at ufl.edu>
To: Bert Gunter <bgunter.4567 at gmail.com>
Cc: R-help <r-help at r-project.org>
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=100000
)
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 <bgunter.4567 at gmail.com>
Sent: Thursday, January 27, 2022 1:17 PM
To: Ebert,Timothy Aaron <tebert at ufl.edu>
Cc: PIKAL Petr <petr.pikal at precheza.cz>; R-help <r-help at
r-project.org>
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.logical(factor(charvec))
as.logical(factor(c(0,1)))? # "0" and "1" give NA
(I mean of course purely internal R code, not export of data to an external
application).
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
Thu, Jan 27, 2022 at 8:12 AM Ebert,Timothy Aaron <tebert at ufl.edu>
wrote:>
> One could use the microbenchmark package to compare which approach is
faster, assuming the dataset is large enough that the outcome will make a
measurable difference.
>
[[alternative HTML version deleted]]