What is the is.unsorted() function in R?

Overview

The is.unsorted() function in R is used to check if a given object passed to it is not sorted. It returns TRUE if the object is not sorted and FALSE if otherwise.

Syntax

is.unsorted(x, na.rm = FALSE, strictly = FALSE)
Syntax for the is.unsorted() function

Parameter values

The is.unsorted() function takes the following parameter values:

  • x: This is any R object of numerical values.
  • na.rm: This takes the logical value (TRUE or FALSE) indicating whether the missing values be removed before checking or not.
  • strictly: This takes the logical value (TRUE or FALSE) indicating whether the check should be strictly increasing values or not.

Return value

The is.unsorted() function returns a length-one logical value.

Example

# creating R objects
a <- c( 1:10, 12:15, 20)
b <- w <- c(12:15, 1:10, 20)
# printing the R objects
a
b
# Checking if the objects are unsorted
is.unsorted(a)
is.unsorted(b)

Code explanation

  • Lines 2–3: We create R objects, a and b, containing numerical values.
  • Lines 6–7: We print the values of the objects, a and b.
  • Lines 10: We check to see if the objects are unsorted using the is.unsorted() function. We print the results to the console.

Free Resources