rank                  package:base                  R Documentation

_S_a_m_p_l_e _R_a_n_k_s

_D_e_s_c_r_i_p_t_i_o_n:

     Returns the sample ranks of the values in a vector.  Ties (i.e.,
     equal values) and missing values can be handled in several ways.

_U_s_a_g_e:

     rank(x, na.last = TRUE,
          ties.method = c("average", "first", "random", "max", "min"))

_A_r_g_u_m_e_n_t_s:

       x: a numeric, complex, character or logical vector.

 na.last: for controlling the treatment of 'NA's. If 'TRUE', missing
          values in the data are put last; if 'FALSE', they are put
          first; if 'NA', they are removed; if '"keep"' they are kept
          with rank 'NA'.

ties.method: a character string specifying how ties are treated, see
          'Details'; can be abbreviated.

_D_e_t_a_i_l_s:

     If all components are different (and no 'NA's), the ranks are well
     defined, with values in 'seq_len(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. The
     '"random"' method puts these in random order whereas the default,
     '"average"', replaces them by their mean, and '"max"' and '"min"'
     replaces them by their maximum and minimum respectively, the
     latter being the typical sports ranking.

     'NA' values are never considered to be equal: for 'na.last = TRUE'
     and 'na.last = FALSE' they are given distinct ranks in the order
     in which they occur in 'x'.

     *NB*: 'rank' is not itself generic but 'xtfrm' is, and
     'rank(xtfrm(x), ....)' will have the desired result if there is a
     'xtfrm' method.  Otherwise, 'rank' will make use of '==', '>' and
     'is.na' methods for classed objects, possibly rather slowly.

_V_a_l_u_e:

     A numeric vector of the same length as 'x' with names copied from
     'x' (unless 'na.last = NA', when missing values are removed).  The
     vector is of integer type unless 'ties.method = "average"' when it
     is of double type (whether or not there are any ties).

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole.

_S_e_e _A_l_s_o:

     'order' and 'sort'.

_E_x_a_m_p_l_e_s:

     (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= "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))

