[Rd] inconsistency between order() and sort()

Hervé Pagès hpages at fredhutch.org
Fri Nov 6 00:05:52 CET 2015


Hi,

There is an obvious relationship between order() and sort():

   > x <- c("b", "c", NA, "a")
   > identical(x[order(x, na.last=TRUE)], sort(x, na.last=TRUE))
   [1] TRUE
   > identical(x[order(x, na.last=FALSE)], sort(x, na.last=FALSE))
   [1] TRUE
   > identical(x[order(x, na.last=NA)], sort(x, na.last=NA))
   [1] TRUE

and having this level of consistency between order-related
operations is good.

However:

   > identical(x[order(x)], sort(x))
   [1] FALSE

This is unfortunate and error prone when writing code that
assumes consistent behavior between order() and sort(). The
problem is that order() and sort() use a different default
for the 'na.last' argument (TRUE and NA, respectively). Is
there any reason for that? Any chance this could be revisited?

Thanks,
H.

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fredhutch.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-devel mailing list