[R] how to test for the empty set

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Nov 24 19:15:35 CET 2008


On 24-Nov-08 17:41:25, G. Jay Kerns wrote:
> Dear R-help,
> I first thought that the empty set (for a vector) would be NULL.
> 
> x <- c()
> x
> 
> However, the documentation seems to make clear that there _many_ empty
> sets depending on the vector's mode, namely, numeric(0), character(0),
> logical(0), etc.  This is borne out by
> 
> y <- letters[1:3]
> z <- letters[4:6]
> intersect(y,z)
> 
> which, of course, is non-NULL:
> 
> is.null(character(0))   # FALSE

In the above (and below) cases, would not

  (length(x)==0)

do?
Ted.

> So, how can we test if a vector is, say, character(0)?  The following
> doesn't (seem to) work:
> 
> x <- character(0)
> x == character(0)  # logical(0)
> 
> More snooping led to the following:
> 
> wiki.r-project.org/rwiki/doku.php?id=tips:surprises:emptysetfuncs
> 
> and at the bottom of the page it says "logical(0) is an empty set,
> thus is TRUE".  However, I get
> 
> isTRUE(logical(0))   # FALSE
> 
> but, on the other hand,
> 
> all.equal(x, character(0))  # TRUE
> 
> This would seem to be the solution, but am I missing something? and in
> particular, is there an elegant way to check in the case that the mode
> of the vector is not already known?
> 
> Thanks in advance for any insight you may have.
> 
> Best,
> Jay

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 24-Nov-08                                       Time: 18:15:31
------------------------------ XFMail ------------------------------



More information about the R-help mailing list