Built with Tutify.io

    AIStudyLabs

    Sort Vector in R - Character, Date, Numeric

    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,

    <a id="h-1-quick-examples"></a>

    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))]
    

    <a id="h-2-sort-vector-syntax"></a>

    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

    <a id="h-3-sort-vector-alphabetically"></a>

    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"
    

    <a id="h-4-sort-vector-by-name"></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
    

    <a id="h-5-sort-vector-by-date"></a>

    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"
    

    <a id="h-conclusion"></a>

    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.

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References