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 (TRUEorFALSE) indicating whether the missing values be removed before checking or not.strictly: This takes the logical value (TRUEorFALSE) 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 objectsa <- c( 1:10, 12:15, 20)b <- w <- c(12:15, 1:10, 20)# printing the R objectsab# Checking if the objects are unsortedis.unsorted(a)is.unsorted(b)
Code explanation
- Lines 2–3: We create R objects,
aandb, containing numerical values. - Lines 6–7: We print the values of the objects,
aandb. - Lines 10: We check to see if the objects are unsorted using the
is.unsorted()function. We print the results to the console.