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

Phillips Rogfield thebudget72 @end|ng |rom gm@||@com
Wed Jun 23 17:06:05 CEST 2021


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?



More information about the R-help mailing list