Learn R Programming

terra (version 1.8-5)

sort: Sort a SpatRaster or SpatVector

Description

Sort the cell values of a SpatRaster across layers. You can also compute the sorting order.

Or sort the records of SpatVector (or data.frame) by specifying the column number(s) or names(s) to sort on.

Usage

# S4 method for SpatRaster
sort(x, decreasing=FALSE, order=FALSE, filename="", ...)

# S4 method for SpatVector sort(x, v, decreasing=FALSE)

Value

SpatRaster

Arguments

x

SpatRaster

decreasing

logical. If TRUE, sorting is in decreasing order

order

logical. If TRUE the sorting order is returned instead of the sorted values

filename

character. Output filename

...

additional arguments for writing files as in writeRaster

v

character or numeric indicating the column(s) to sort on

Examples

Run this code
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
r <- c(r, r/2, r*2)
sort(r)

ord <- sort(r, order=TRUE)
# these two are the same 
ord[[1]]
which.min(r)

Run the code above in your browser using DataLab