ID EN
Vector & List

sort

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Urutkan (atau urutkan) vektor atau faktor (sebagian) ke dalam urutan menaik atau menurun. Untuk mengurutkan lebih dari satu variabel, misalnya untuk mengurutkan bingkai data, lihat urutan.

Syntax

R
sort(x, decreasing = FALSE, &#8230;)<p></p><p># S3 method for default
sort(x, decreasing = FALSE, na.last = NA, &#8230;)</p><p>sort.int(x, partial = NULL, na.last = NA, decreasing = FALSE,
         method = c("auto", "shell", "quick", "radix"), index.return = FALSE)</p>

Arguments

Parameter Deskripsi
x for sort an R object with a class or a numeric, complex, character or logical vector. For sort.int, a numeric, complex, character or logical vector, or a factor.
decreasing logical. Should the sort be increasing or decreasing? For the "radix" method, this can be a vector of length equal to the number of arguments in …. For the other methods, it must be length one. Not available for partial sorting.
&#8230; arguments to be passed to or from methods or (for the default methods and objects without a class) to sort.int.
na.last for controlling the treatment of NAs. If TRUE, missing values in the data are put last; if FALSE, they are put first; if NA, they are removed.
partial NULL or a vector of indices for partial sorting.
method character string specifying the algorithm used. Not available for partial sorting. Can be abbreviated.
index.return logical indicating if the ordering index vector should be returned as well. Supported by method == "radix" for any na.last mode and data type, and the other methods when na.last = NA (the default) and fully sorting non-factors.

Return Value

Untuk sortir, hasilnya tergantung pada metode S3 yang dikirimkan. Jika x tidak memiliki kelas sort.int digunakan dan deskripsinya berlaku. Untuk objek yang dikelompokkan yang tidak memiliki metode tertentu, metode default akan digunakan dan setara dengan x[order(x, ...)]: ini bergantung pada kelas yang memiliki metode yang sesuai untuk [ (dan urutan tersebut juga akan berfungsi, yang memerlukan metode xtfrm). Untuk sort.int nilainya adalah vektor yang diurutkan kecuali index.return benar, jika hasilnya berupa daftar dengan komponen

Details

sort adalah fungsi umum dimana metode dapat ditulis, dan sort.int adalah metode internal yang kompatibel dengan S jika hanya tiga argumen pertama yang digunakan. Metode pengurutan default menggunakan urutan untuk objek yang dikelompokkan, yang pada gilirannya menggunakan fungsi generik xtfrm (dan bisa lambat kecuali metode xtfrm telah ditentukan atau is.numeric(x) benar). Nilai kompleks diurutkan terlebih dahulu berdasarkan bagian nyata, kemudian bagian imajiner. Metode "otomatis" memilih "radix" untuk vektor numerik pendek (kurang dari \(2^{31}\)), vektor bilangan bulat, vektor logika, dan faktor; jika tidak, "cangkang". Kecuali untuk metode "radix", urutan untuk vektor karakter akan bergantung pada urutan penyusunan lokal yang digunakan: lihat Perbandingan. Urutan faktor adalah urutan levelnya (

Contoh

Example
R
# NOT RUN {
require(stats)

x <- swiss$Education[1:25]
x; sort(x); sort(x, partial = c(10, 15))

## illustrate 'stable' sorting (of ties):
sort(c(10:3, 2:12), method = "shell", index.return = TRUE) # is stable
## $x : 2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10 11 12
## $ix: 9  8 10  7 11  6 12  5 13  4 14  3 15  2 16  1 17 18 19
sort(c(10:3, 2:12), method = "quick", index.return = TRUE) # is not
## $x : 2  3  3  4  4  5  5  6  6  7  7  8  8  9  9 10 10 11 12
## $ix: 9 10  8  7 11  6 12  5 13  4 14  3 15 16  2 17  1 18 19

x <- c(1:3, 3:5, 10)
is.unsorted(x)                  # FALSE: is sorted
is.unsorted(x, strictly = TRUE) # TRUE : is not (and cannot be)
                                # sorted strictly
# }
# NOT RUN {
## Small speed comparison simulation:
N <- 2000
Sim <- 20
rep <- 1000 # << adjust to your CPU
c1 <- c2 <- numeric(Sim)
for(is in seq_len(Sim)){
  x <- rnorm(N)
  c1[is] <- system.time(for(i in 1:rep) sort(x, method = "shell"))[1]
  c2[is] <- system.time(for(i in 1:rep) sort(x, method = "quick"))[1]
  stopifnot(sort(x, method = "shell") == sort(x, method = "quick"))
}
rbind(ShellSort = c1, QuickSort = c2)
cat("Speedup factor of quick sort():\n")
summary({qq <- c1 / c2; qq[is.finite(qq)]})

## A larger test
x <- rnorm(1e7)
system.time(x1 <- sort(x, method = "shell"))
system.time(x2 <- sort(x, method = "quick"))
system.time(x3 <- sort(x, method = "radix"))
stopifnot(identical(x1, x2))
stopifnot(identical(x1, x3))
# }

See Also

‘Comparison’ for how character strings are collated. order for sorting on or reordering multiple variables. is.unsorted. rank.