RE: [R] reason for na.last=TRUE in rank

From: McGehee, Robert <Robert.McGehee_at_geodecapital.com>
Date: Fri 20 May 2005 - 06:02:55 EST


Because rank and order are (supposed to be) inverses of each other.

For example:
> a <- c(3, 1, NA)
> a[order(a[rank(a)])]

[1] 3 1 NA
> a[rank(a[order(a)])]

[1] 3 1 NA

BUT
> a[order(a[rank(a, na.last = FALSE)])]
[1] 1 NA 3

> a[rank(a[order(a)], na.last = FALSE)]
[1] 1 NA 3

-----Original Message-----
From: Alejandro Munoz del Rio [mailto:munoz@stat.wisc.edu] Sent: Thursday, May 19, 2005 1:01 PM
To: r-help@stat.math.ethz.ch
Subject: [R] reason for na.last=TRUE in rank

Dear UseRs,

Could someone explain to me why the default behaviour of rank() is to assign the
largest rank to missing data

> rank(c(3, 1, NA))
[1] 2 1 3

as opposed to what I would hazard would be the expected 2, 1, NA?

Despite consistency being the hobgoblin of little minds, of two closely related functions one handles NAs in the same way (order()) but another one doesn't (sort()). order() also uses the "NA last" rule by default, whereas sort() removes NAs.

alejandro



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri May 20 06:05:58 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:55 EST