Skip to content
Prev 343891 / 398498 Next

some question about vector[-NULL]

On 10/09/2014, 9:53 PM, PO SU wrote:
If you know that you want to omit elements 1 and 3, then negative
integer indexing is safe.  You'll never need to explicitly type out the
whole logical vector.

The only problem with it is when you construct the indices by tests, and
sometimes nothing matches the tests, so you end up with an empty vector
or NULL.  In that case you can just as easily construct the logical
vector.

If your vectors are so long that you are worried about the cost of
constructing a logical vector that long, then it's probably worthwhile
checking the length of the integer index explicitly, i.e. instead of

a <- a[ -i ]

use

if (any(i > 0)) a <- a[ -i ]

Duncan Murdoch