Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Avi Gross via R-help
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread William Michels via R-help
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"

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Jeff Newmiller
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Bill Dunlap
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Jeff Newmiller
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Phillips Rogfield
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Bert Gunter
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Avi Gross via R-help
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Bill Dunlap
> 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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Eric Berger
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

Re: [R] How to spot/stop making the same mistake

2021-06-23 Thread Jeff Newmiller
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

[R] How to spot/stop making the same mistake

2021-06-23 Thread Phillips Rogfield
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