Skip to content
Prev 157131 / 398506 Next

Exclusion of elements in a vector

On Thu, Sep 25, 2008 at 8:15 AM, Chuck Cleland <ccleland at optonline.net> wrote:
Yes.  I'm not sure what you want to do can be done in a clean way.
The problem is with the semantics of the indexing operator for numeric
indices.  A "natural" way of expressing the empty numeric vector is

numeric(0)

When you use a numeric vector as a set of indices the first check is
to determine if all the values are negative, in which case the indices
are used to exclude rather than include.  However, you can't tell
whether the elements of the empty numeric vector are positive or
negative because there aren't any elements.  Thus

vector[numeric(0)]

is the same as

vector[-numeric(0)]
integer(0)
integer(0)

The error message you were getting results from the class of NULL,
which is different from the class of numeric(0).  Arithmetic
operations are defined, to some extent, for numeric(0) but not for
NULL.