roject.org; Phillips Rogfield ; Bert
Gunter ; Eric Berger
Cc: r-help@r-project.org
Subject: Re: [R] How to spot/stop making the same mistake
For the record, `!!` is not an operator so it does not "operate" on anything.
The right ! does per the help page (?`!`) interpret non-zero values as T
Hi Phillips,
Maybe these examples will be useful:
> vec <- c("a","b","c","d","e")
> vec[c(1,1,1,0,0)]
[1] "a" "a" "a"
> vec[c(1,1,1,2,2)]
[1] "a" "a" "a" "b" "b"
> vec[c(5,5,5,5,5)]
[1] "e" "e" "e" "e" "e"
> vec[c(NA,NA,NA,0,0,0,0)]
[1] NA NA NA
> vec[c(NA,NA,NA,1,1,1,1)]
[1] NA NA NA "a" "a"
IMO that puts the cart before the horse. rlang implements these "operators" in
very focused situations using custom expression parsing, and if they cannot
tell the difference between numeric/logical and character data then rlang is
broken.
That said, I do think as.logical() is more direct than
Note that !! and !!! are special operators involving "quasiquotation" in
the dplyr package.
I would use as.logical(x) instead of !!x since its meaning is clear to any
user.
-Bill
On Wed, Jun 23, 2021 at 11:13 AM Jeff Newmiller
wrote:
> For the record, `!!` is not an operator so it does not "op
For the record, `!!` is not an operator so it does not "operate" on anything.
The right ! does per the help page (?`!`) interpret non-zero values as TRUE and
invert that logic, yielding a logical result even if the input is not logical.
The left ! inverts that again, yielding a logical vector wi
Dear all,
thank for for your suggestion.
Yes I come from languages where 1 means TRUE and 0 means FALSE. In
particular from C/C++ and Python.
Evidently this is not the case for R.
In my mind I kind took for granted that that was the case (1=TRUE, 0=FALSE).
Knowing this is not the case for R m
Just as a way to save a bit of typing, instead of
> as.logical(0:4)
[1] FALSE TRUE TRUE TRUE TRUE
> !!(0:4)
[1] FALSE TRUE TRUE TRUE TRUE
DO NOTE that the parentheses in the second expression should never be
omitted, a possible reason to prefer the as.logical() construction.
Also note th
time, you can substitute ensure_boolean(vec)
-Original Message-
From: R-help On Behalf Of Jeff Newmiller
Sent: Wednesday, June 23, 2021 11:18 AM
To: r-help@r-project.org; Phillips Rogfield ;
r-help@r-project.org
Subject: Re: [R] How to spot/stop making the same mistake
I practically
> a variable that equals 1 for the elements I want to select:
>
> t = c(1,1,1,0,0)
How do you typically make such a variable? If you use something like
t <- ifelse(x == "Yes", 1, 0)
you should instead use
t <- x == "Yes"
Naming the variable something like 'isYes' instead of 't' might help
In my code, instead of 't', I name a vector of indices with a meaningful
name, such as idxV, to make it obvious.
Alternatively, a minor change in your style would be to replace your
definition of t by
t <- as.logical(c(1,1,1,0,0))
HTH,
Eric
On Wed, Jun 23, 2021 at 6:11 PM Phillips Rogfield
wr
I practically never construct vectors like your `t` so it isn't a problem. And
since I make a habit of verifying the types of all vectors I am using in
expressions, if it did come up I would notice.
On June 23, 2021 8:06:05 AM PDT, Phillips Rogfield
wrote:
>I make the same mistake all over aga
I make the same mistake all over again.
In particular, suppose we have:
a = c(1,2,3,4,5)
and a variable that equals 1 for the elements I want to select:
t = c(1,1,1,0,0)
To select the first 3 elements.
The problem is that
a[t]
would repeat the first element 3 times .
I have to either
12 matches
Mail list logo