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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.