[Rd] Inconsistency: sort(NULL)/sort.int(NULL) does not throw an error, cf. order(NULL), sort.int(NULL, index.return=TRUE), ...

Henrik Bengtsson hb at biostat.ucsf.edu
Mon Apr 4 06:11:26 CEST 2011


Hi,

while backtracking why sort(NULL) gives a warning, and acknowledging
that NULL is not the same as an empty vector and sort(NULL) is bad
coding, I discovered the following inconsistency of sort.int():

> x <- NULL

> sort(x)
Warning in is.na(x) :
  is.na() applied to non-(list or vector) of type 'NULL'
NULL

> sort.int(x)
Warning in is.na(x) :
  is.na() applied to non-(list or vector) of type 'NULL'
NULL

> sort.int(x, index.return=TRUE)
Warning in is.na(x) :
  is.na() applied to non-(list or vector) of type 'NULL'
Error in sort.list(x, decreasing = decreasing) :
  argument 1 is not a vector

> sort.int(x, partial=1)
Warning in is.na(x) :
  is.na() applied to non-(list or vector) of type 'NULL'
Error in sort.int(NULL, partial = 1) : only atomic vectors can be sorted

> order(x)
Error in order(x) : argument 1 is not a vector


Using an empty vector behaves correctly, e.g. x <- character(0).

For consistency, should sort.int(NULL) also give an error?  If so,
that would result in sort(NULL) throwing an error too, which might
have lots of implications to code/package using NULL as an empty
vector.


Alternatively, if leaving sort.int(NULL) as is, maybe it would make
sense to remove the non-informative warning, e.g. by having sort.int()
testing for:

if (is.null(x) && (!is.null(partial) && !index.return)) {
  return(NULL);
}

or replace it with an informative warning on trying to use sort.int()
to sort NULL.

The above is the case for R version 2.13.0 beta (2011-03-31 r55221)
and R version 2.14.0 Under development (unstable) (2011-04-02 r55254).

/Henrik



More information about the R-devel mailing list