R sort Function


sort() function sorts a vector.

sort(x, decreasing = FALSE, na.last = NA, ...)


x: vector
decreasing: decrease or not
na.last: if TRUE, NAs are put at last position, FALSE at first, if NA, remove them (default)
...

Sort Vectors:

>x <- c(1,2.3,2,3,4,8,12,43,-4,-1,NA)
>sort(x)

[1] -4.0 -1.0 1.0 2.0 2.3 3.0 4.0 8.0 12.0 43.0


>sort(x,decreasing=TRUE)

[1] 43.0 12.0 8.0 4.0 3.0 2.3 2.0 1.0 -1.0 -4.0


>sort(x,decreasing=TRUE, na.last=TRUE)

[1] 43.0 12.0 8.0 4.0 3.0 2.3 2.0 1.0 -1.0 -4.0 NA


>sort(x,decreasing=TRUE, na.last=FALSE)

[1] NA 43.0 12.0 8.0 4.0 3.0 2.3 2.0 1.0 -1.0 -4.0


order() is a similar function. It can sort a vector by its index.



endmemo.com © 2024  | Terms of Use | Privacy | Home