The “rank” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Computes the sample ranks of the values in a numeric vector, with ties getting the average of ranks for the tied values.

  • General Class: Data Transformation

  • Required Argument(s):

    • x: A numeric vector.

  • Notable Optional Arguments:

    • ties.method: A character string specifying how ties are treated. The default is “average,” which assigns the average of the ranks to all the tied values. Other options include “first”, “last”, “random”, and “min”.

  • Example:

  • # Example data for using the rank function
    scores <- c(85, 92, 78, 92, 88)

    # Use rank to compute the sample ranks of the scores
    ranked_scores <- rank(scores)

    # Display the result
    print(ranked_scores)

  • In this example, the rank function is used to compute the sample ranks of a vector of scores (scores). The resulting vector (ranked_scores) contains the ranks of the scores, with ties receiving the average of the ranks. The rank function is commonly used when you need to assign ranks to values, and it provides flexibility in handling tied values through the ties.method argument.

Previous
Previous

The “unique” Function in R

Next
Next

The “order” Function in R