The “sort” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Sorts the elements of a vector or other indexed data structure in ascending or descending order.

  • General Class: Data Transformation

  • Required Argument(s):

    • x: A vector or other indexable object.

  • Notable Optional Arguments:

    • decreasing: Logical. If TRUE, sorts in descending order; if FALSE (default), sorts in ascending order.

  • Example:

  • # Example data for using the sort function
    unsorted_vector <- c(5, 2, 8, 1, 3)

    # Use sort to sort the elements of a vector in ascending order
    sorted_vector <- sort(unsorted_vector)

    # Display the result
    print(sorted_vector)

  • In this example, the sort function is used to sort the elements of a numeric vector (unsorted_vector) in ascending order. The resulting vector (sorted_vector) contains the elements sorted from smallest to largest. The sort function is commonly used for arranging elements in ascending or descending order.

Previous
Previous

The “order” Function in R

Next
Next

The “rev” Function in R