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

From: Henrik Bengtsson <hb_at_biostat.ucsf.edu>
Date: Sun, 03 Apr 2011 21:11:26 -0700


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



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Mon 04 Apr 2011 - 04:16:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 04 Apr 2011 - 07:00:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive