rank
Ranks of Data

Description

Returns a vector of the ranks of the input.

Usage

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

Arguments

x a numeric vector. Missing values (NAs) are allowed.
na.last a vector with one element.
  • If na.last = TRUE, missing values are placed last.
  • If na.last = FALSE, missing values are placed first.
  • If na.last = NA, missing values are deleted.
  • If na.last = "keep", the rank of any missing value is returned as NA.
ties.method a character string. Can be one of the following:
  • "average" indicates that all ties of a specific value are assigned the same rank value: the average of the positions of the ties in the sorted vector.
  • "first" indicates that the ties are assigned ranks from the sequence position.first:position.last, where position.first is the first position of the ties in the sorted vector and position.last is the last.
  • "last" indicates that the ties are assigned ranks from the sequence position.last:position.first.
  • "random" indicates that the ties are assigned the ranks sample(position.first:position.last).
  • "max" indicates that the ties are assigned the rank position.last.
  • "min" indicates that the ties are assigned the rank position.first. This rank is used in many reports of sport results.

Details

The treatment of missing values is controlled by na.last.
For information about estimating the rank of a matrix, see svd, qr or chol.
Value
returns the ranks. That is, the ith value is the rank of x[i]. In case of ties, rank is assigned depending on the ties.method.
Warning
If na.last=NA, the ith value of the result corresponds to the ith element of the vector that is the result of removing NAs from x.
See Also
cor, order, qr, quantile, sort, svd, testscores.
Examples
# Create sample objects:
testscores <- matrix(sample(30:98, 60, replace = TRUE), 30, 2)
diffgeom <- testscores[,1] 
complexanal <- testscores[,2] 
rank(diffgeom)

# Spearman's rank correlation between two sets of testscores: cor(rank(diffgeom), rank(complexanal))

# Create sample objects with missing values: a <- c(4, 2, 5, 1, 4, NA, 6) b <- c(4, 2, 5, NA, 4, 5, 6)

# This does not correctly compute the Spearman's rank correlation of the # non-missing data: cor(rank(a), rank(b)) # [1] 0.1651446

# This does compute the Spearman's rank correlation of non-missing data: cor(rank(a[!is.na(a*b)]),rank(b[!is.na(a*b)])) # # [1] 1

Package base version 6.1.1-7
Package Index