Let's say we have a statement that produces integer(0)
, e.g.
a <- which(1:3 == 5)
What is the safest way of catching this?
Let's say we have a statement that produces integer(0)
, e.g.
a <- which(1:3 == 5)
What is the safest way of catching this?
If it's specifically zero length integers, then you want something like
Check it with:
You can also use
assertive
for this.On second thought I think any is more beautiful than
length(.)
:Maybe off-topic, but R features two nice, fast and empty-aware functions for reducing logical vectors --
any
andall
:Inspired by Andrie's answer, you could use
identical
and avoid any attribute problems by using the fact that it is the empty set of that class of object and combine it with an element of that class:Or more generally:
That is R's way of printing a zero length vector (an integer one), so you could test for
a
being of length 0:It might be worth rethinking the strategy you are using to identify which elements you want, but without further specific details it is difficult to suggest an alternative strategy.