Sorting of a Vector in R Programming - sort() Function Last Updated : 09 Nov, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In R Programming Language we can sort a vector in ascending or descending order using the sort() function. The sort() function returns a sorted version of the input vector. sort() function in is used to sort a vector by its values. It takes the Boolean value as an argument to sort in ascending or descending order. Syntax: sort(x, decreasing, na.last) Parameters:x: Vector to be sorted decreasing: Boolean value to sort in descending order na.last: Boolean value to put NA at the end R program to sort a vector Example 1: R # Creating a vector x <- c(7, 4, 3, 9, 1.2, -4, -5, -8, 6, NA) # Calling sort() function sort(x) Output: [1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0Example 2: R # Creating a vector x <- c(7, 4, 3, 9, 1.2, -4, -5, -8, 6, NA) # Calling sort() function # to print in decreasing order sort(x, decreasing = TRUE) # Calling sort() function # to print NA at the end sort(x, na.last = TRUE) Output: [1] 9.0 7.0 6.0 4.0 3.0 1.2 -4.0 -5.0 -8.0[1] -8.0 -5.0 -4.0 1.2 3.0 4.0 6.0 7.0 9.0 NAExample 3: R # Create a character vector names <- c("Vipul", "Raj", "Singh", "Jhala") # Sort the vector in alphabetical order sorted_names <- sort(names) print(sorted_names) Output: [1] "Jhala" "Raj" "Singh" "Vipul" Comment More infoAdvertise with us Next Article Sorting of a Vector in R Programming - sort() Function N nidhi_biet Follow Improve Article Tags : R Language R Vector-Function Similar Reads Reordering of a Data Set in R Programming - arrange() Function arrange() function in R Language is used for reordering of table rows with the help of column names as expression passed to the function. Syntax: arrange(x, expr) Parameters: x: data set to be reordered expr: logical expression with column name Example 1: Python3 1== # R program to reorder a data se 1 min read Creating a Vector of sequenced elements in R Programming - seq() Function In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct 2 min read Types of Vectors in R Programming Vectors in R programming are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from â1â and not from â0â. Vectors are the most basic data types in R. Even a single object created i 5 min read Sorting of Arrays in R Programming Prerequisite: R â Array A vector is a uni-dimensional array, which is specified by a single dimension, length. A Vector can be created using the âc()â function. A list of values is passed to the c() function to create a vector. Sorting can be done either in ascending order or descending. There are f 5 min read Calculate Rank of the Values of a Vector in R Programming - rank() Function rank() function in R Language is used to return the sample ranks of the values of a vector. Equal values and missing values are handled in multiple ways. Syntax: rank(x, na.last) Parameters: x: numeric, complex, character, and logical vector na.last: Boolean Value to remove NAs Example 1: Python3 1= 1 min read Like