Recently, I reviewed a paper that introduces a package, so I reviewed the code and noticed that the authors
often used &&
(and) and ||
(or) whereas only &
and |
were required. I guess this is due to:
- other programming language;
- the fact that for vectors of one element in R
&
and&&
are equivalent (same for|
and||
).
Regarding point 1, we could take C as an example. In C, &
and |
are bitewise operators while &&
are ||
logical ones
for instance 4 & 7
gives 1
but 4 && 7
gives TRUE
. In R, things are a
bit different: &&
and ||
only test the first element of two vectors:
|
|
whereas &
and |
perform logical tests element-wise:
|
|
and a warning signal is returned when vector size do not match:
|
|
Using &&
and ||
for vectors of 1 element may not be a big deal after all,
but if you are actually trying to do element-wise logical tests and you use
&&
and ||
it could easily generate errors that go under the radar, so you
should better be aware of this!