Returns the sample ranks of the values in a vector. Ties (i.e., equal values) and missing values can be handled in several ways.
a numeric, complex, character or logical vector.
for controlling the treatment of
a character string specifying how ties are treated, see ‘Details’; can be abbreviated.
If all components are different (and no
NAs), the ranks are
well defined, with values in
seq_along(x). With some values equal
(called ‘ties’), the argument
ties.method determines the
result at the corresponding indices. The
"first" method results
in a permutation with increasing values at each index set of ties, and
"first" with decreasing values. The
"random" method puts these in random order whereas the
"average", replaces them by their mean, and
"min" replaces them by their maximum and
minimum respectively, the latter being the typical sports
NA values are never considered to be equal: for
na.last = FALSE they are given distinct ranks in
the order in which they occur in
rank is not itself generic but
rank(xtfrm(x), ....) will have the desired result if
there is a
xtfrm method. Otherwise,
rank will make use
is.na and extraction methods for
classed objects, possibly rather slowly.
A numeric vector of the same length as
x with names copied from
na.last = NA, when missing values are
removed). The vector is of integer type unless
x is a long
ties.method = "average" when it is of double type
(whether or not there are any ties).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
xtfrm, see above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
(r1 <- rank(x1 <- c(3, 1, 4, 15, 92))) x2 <- c(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5) names(x2) <- letters[1:11] (r2 <- rank(x2)) # ties are averaged ## rank() is "idempotent": rank(rank(x)) == rank(x) : stopifnot(rank(r1) == r1, rank(r2) == r2) ## ranks without averaging rank(x2, ties.method= "first") # first occurrence wins rank(x2, ties.method= "last") # last occurrence wins rank(x2, ties.method= "random") # ties broken at random rank(x2, ties.method= "random") # and again ## keep ties ties, no average (rma <- rank(x2, ties.method= "max")) # as used classically (rmi <- rank(x2, ties.method= "min")) # as in Sports stopifnot(rma + rmi == round(r2 + r2)) ## Comparing all tie.methods: tMeth <- eval(formals(rank)$ties.method) rx2 <- sapply(tMeth, function(M) rank(x2, ties.method=M)) cbind(x2, rx2) ## ties.method's does not matter w/o ties: x <- sample(47) rx <- sapply(tMeth, function(MM) rank(x, ties.method=MM)) stopifnot(all(rx[,1] == rx))
We want your feedback!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.