[R] behaviour of all(NULL == c("a", "b"))

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Feb 9 17:47:24 CET 2005


Matthias Burger <matthias.burger at epigenomics.com> writes:

> Hi all,
> 
> I'm a little surprised at
>  > NULL == c("a", "b")
> logical(0)
>  > all(NULL == c("a", "b"))
> [1] TRUE
> 
> Reading the documentation for all() this was not clear for me to be expected.

all() over a vector of length zero is TRUE for much the same reason
that prod(numeric(0)) is 1 which in turn is related to sums over empty
sets being zero. NULL has length zero so comparisons with it makes a
logical vector of length zero....

> Originally the question came up when using
>  > match.arg(NULL, c("a", "b"))
> [1] "a"
> where I had thought an error would occur.
> 
> So could someone please help me and explain what I have overlooked.
> Should I realy have to use is.null() as a precondition check here when a NULL
> argument could arise.
> 

Probably yes, unless you happen to like that behaviour (not entirely
unlikely in some cases).

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list