[R] How to spot/stop making the same mistake
Avi Gross
@v|gro@@ @end|ng |rom ver|zon@net
Wed Jun 23 17:45:25 CEST 2021
This is unfortunately a bad habit many of us got from earlier languages like
the C group of languages where 0 is FALSE and 1 (and anything non-zero) is
TRUE. A language like Python is arguably even worse in that all kinds of
things can be TRUE or FALSE in odd ways, like a non-empty string or even a
random object which has declared how to decide if it qualifies as true.
R here has an issue with having so many ways to index a vector so that use
of integers has another meaning and only indexing by Booleans has the
meaning you want. So, you just need to adjust your mindset or perhaps write
a little silly function like ensure_boolean() that checks if the vector it
has is ALL just full of zero or 1 with probably no NA and otherwise returns
the vector unchanged. If it is all 0/1, it converts it and returns the
Boolean equivalent. Then instead of supplying the vector directly, most of
the time, you can substitute ensure_boolean(vec) ....
-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Jeff Newmiller
Sent: Wednesday, June 23, 2021 11:18 AM
To: r-help using r-project.org; Phillips Rogfield <thebudget72 using gmail.com>;
r-help using r-project.org
Subject: Re: [R] How to spot/stop making the same mistake
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 <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 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