Check if an Object is sorted or not in R Programming – is.unsorted() Function
Last Updated :
24 Jun, 2020
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:
x < - c( 1 : 9 )
mat < - matrix(c( 5 , 3 , 4 , 2 ), 2 , 2 )
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:
x < - c( 2 , 4 , 2 , 5 , 7 , 1 , 3 , 8 , 1 )
x
is .unsorted(x)
x1 < - sort(x)
x1
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...