Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 60
4.3.3.3 Reordering and Sorting
ОглавлениеTo sort a vector, we can use the function sort()
.
sorting
sort()
# Example 1: v1 <- c(1, -4, 2, 0, pi) sort(v1) ## [1] -4.000000 0.000000 1.000000 2.000000 3.141593 # Example 2: To make sorting meaningful, all variables are coerced to # the most complex type: v1 <- c(1:3, 2 + 2i) sort(v1) ## [1] 1+0i 2+0i 2+2i 3+0i # Sorting is per increasing numerical or alphabetical order: v3 <- c("January", "February", "March", "April") sort(v3) ## [1] "April" "February" "January" "March" # Sort order can be reversed: sort(v3, decreasing = TRUE) ## [1] "March" "January" "February" "April"