Check if an Object is sorted or not in R Programming – is.unsorted() Function
is.unsorted()
function in R Language is used to check if an object is sorted or not. It returns False if the object is sorted otherwise True.
Syntax: is.unsorted(x)
Parameters:
x: Object
Example 1:
# R Program to check if # an object is sorted # Creating a vector x < - c( 1 : 9 ) # Creating a matrix mat < - matrix(c( 5 , 3 , 4 , 2 ), 2 , 2 ) # Calling is.unsorted() Function is .unsorted(x) is .unsorted(mat) |
Output:
[1] FALSE [1] TRUE
Here, the output is FALSE for vector because it is sorted and matrix is unsorted, hence the function returns TRUE for matrix.
Example 2:
# R Program to check if # an object is sorted # Creating a vector x < - c( 2 , 4 , 2 , 5 , 7 , 1 , 3 , 8 , 1 ) x # Calling is.unsorted() Function is .unsorted(x) # Sorting the function x1 < - sort(x) x1 # Calling is.unsorted() Function is .unsorted(x1) |
Output:
[1] 2 4 2 5 7 1 3 8 1 [1] TRUE [1] 1 1 2 2 3 4 5 7 8 [1] FALSE
Please Login to comment...