[R] How to spot/stop making the same mistake
Avi Gross
@v|gro@@ @end|ng |rom ver|zon@net
Thu Jun 24 02:23:15 CEST 2021
Just a caution. There IS an operator of `!!` in the tidyverse called "bang bang" that does a kind of substitution and you can look up the help page for it as:
?`!!`
I just tried it on an example and it definitely will in some cases do this other evaluation.
I doubt this will clash, but of course parentheses can force the normal evaluation as in !(!(a))
And there is a !!! symbol there too to make a big bang, theoretically.
But not for novices 😉
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller
Sent: Wednesday, June 23, 2021 2:10 PM
To: r-help using r-project.org; Phillips Rogfield <thebudget72 using gmail.com>; Bert Gunter <bgunter.4567 using gmail.com>; Eric Berger <ericjberger using gmail.com>
Cc: r-help using 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 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 without the inversion.
On June 23, 2021 10:39:07 AM PDT, Phillips Rogfield <thebudget72 using gmail.com> wrote:
>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 makes things simpler.
>
>Mine was just an example, sometimes I load datasets taken from outside
>and variables are coded with 1/0 (for example, a treatment variable may
>
>be coded that way).
>
>I also did not know the !!() syntax!
>
>Thank you for your help and best regards.
>
>On 23/06/2021 17:55, Bert Gunter wrote:
>> 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 that !! "acts [only] on raw, logical and number-like
>> vectors," whereas as.logical() is more general. e.g. (from ?logical):
>>
>> > charvec <- c("FALSE", "F", "False", "false", "fAlse", "0",
>> + "TRUE", "T", "True", "true", "tRue", "1")
>> > as.logical(charvec)
>> [1] FALSE FALSE FALSE FALSE NA NA TRUE TRUE TRUE TRUE
> NA
>> NA
>> > !!charvec
>> Error in !charvec : invalid argument type
>>
>>
>> Cheers,
>> Bert
>>
>> 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, Jun 23, 2021 at 8:31 AM Eric Berger <ericjberger using gmail.com
>> <mailto:ericjberger using gmail.com>> wrote:
>>
>> 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
>> <thebudget72 using gmail.com <mailto:thebudget72 using gmail.com>>
>> wrote:
>>
>> > 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 convert `t` to boolean:
>> >
>> > a[t==1]
>> >
>> > Or use `which`
>> >
>> > a[which(t==1)]
>> >
>> > How can I "spot" this error?
>> >
>> > It often happens in long scripts.
>> >
>> > Do I have to check the type each time?
>> >
>> > Do you have any suggestions?
>> >
>> > ______________________________________________
>> > R-help using r-project.org <mailto:R-help using r-project.org> mailing list
>> -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> <https://stat.ethz.ch/mailman/listinfo/r-help>
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> <http://www.R-project.org/posting-guide.html>
>> > and provide commented, minimal, self-contained, reproducible
>code.
>> >
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org <mailto:R-help using r-project.org> mailing list
>--
>> To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> <https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible
>code.
>>
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help using 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 using 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.
More information about the R-help
mailing list