You are currently viewing Sort Vector in R – Character, Date, Numeric

Use the sort() function to sort vector elements in R by ascending or descending order. This sort() function takes an R vector as an argument and results in the sorted vector by default in ascending order. Use decreasing=TRUE param to sort vectors in descending order.

If you have a character vector, the sort() function in R by default sort vector values by alphabetical order. Note that sort doesn’t order the vector in place meaning the original vector is not affected instead it returns a sorted version of the input object.

In this article, I will explain sorting character vector, numeric vector, vector by name, sort by date values, and getting an index of sorted vector e.t.c,

1. Quick Examples

Following are quick examples of how to sort vector values.


# Quick Examples of sorting vector

# Sort Character Vector
v <- c('D','R','G','A','X')
sort(v)
sort(v, decreasing=TRUE)

# Sort Numeric Vector
v <- c(3,4,1,5,2)
sort(v)

# Sort Date Vector
dates<-c("2022-01-06","2022-01-02",
        "2022-01-04","2022-01-07")
dates <- as.Date(dates)
sort(dates)
sort(dates,decreasing=TRUE)

# Sort Named Vector
v <- c(c1=2,c5=6,c3=3,c2=1)
v[sort(names(v))]

2. Sort() Vector Syntax

Following is the syntax of the sort() function. Use order() function to get the index values of the sorted vector.


# sort() Function Syntax
sort(x, decreasing = FALSE, ...)

Parameters

  • x – Input vector. It can be of type character, numeric, logical e.t.c.
  • decreasing – Logical value. By default set to FALSE. To sort by ascending order set it to TRUE.
  • ...

3. Sort Vector Alphabetically

To sort a vector alphabetically in R using the sort() function that takes a vector as an argument and returns an alphabetically ordered value for the character vector and ascending order for numeric. Use c() function to create vector.

To sort a vector alphabetically, use the sort() function, which accepts a vector as its argument. This function returns the vector with character elements in alphabetical order and numeric elements in ascending order. Use the c() function to create vector.


# Sort Character Vector
v <- c('D','R','G','A','X')
sort(v)

# Output
#> sort(v)
#[1] "A" "D" "G" "R" "X"

To sort vectors by descending order use decreasing=TRUE param.


# Decreasing order
sort(v, decreasing=TRUE)

# Output
#[1] "X" "R" "G" "D" "A"

4. Sort Vector by Name

Use names() function to get the names of the vector and use it on sort() to order them and finally use R base [] notation to get the vector sorted by name. In the below example I have used a named vector and sorted the vector by names.


# Named Vector
v <- c(c1=2,c5=6,c3=3,c2=1)
v[sort(names(v))]

# Output
#c1 c2 c3 c5 
# 2  1  3  6 

5. Sort Vector By Date

To sort a vector by date values first you need to have a vector of type date. Either you can create a date vector or convert the vector to date type using as.Date() method. Once you have the date vector pass it to sort() to order values by ascending order or use order() method to get only indices.

The following creates a vector with date values and converts it to date type using as.Date() function and finally sort by date.


# Sort date vector
dates<-c("2022-01-06","2022-01-02",
        "2022-01-04","2022-01-07")
dates <- as.Date(dates)
sort(dates)

# Output
#[1] "2022-01-02" "2022-01-04" "2022-01-06" "2022-01-07"

Sort vector in descending order.


# sort date vector descending order.
sort(dates,decreasing=TRUE)

# Output
[1] "2022-01-07" "2022-01-06" "2022-01-04" "2022-01-02"

Conclusion

In this article, you have learned the usage of sort() method and how to use it to sort numeric vector in ascending or descending order, sort character vector alphabetically, sort by name, date values e.t.c. You can download this example from Github R Programming Examples Project.

References