ID EN
Vector & List

cut

R Base 3.6.2 🇮🇩 Bahasa Indonesia

cut membagi rentang x menjadi beberapa interval dan mengkodekan nilai dalam x sesuai dengan intervalnya. Interval paling kiri berhubungan dengan level satu, interval paling kiri berikutnya berhubungan dengan level dua, dan seterusnya.

Syntax

R
cut(x, &#8230;)<p></p><p># S3 method for default
cut(x, breaks, labels = NULL,
    include.lowest = FALSE, right = TRUE, dig.lab = 3,
    ordered_result = FALSE, &#8230;)</p>

Arguments

Parameter Deskripsi
x a numeric vector which is to be converted to a factor by cutting.
breaks either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which x is to be cut.
labels labels for the levels of the resulting category. By default, labels are constructed using "(a,b]" interval notation. If labels = FALSE, simple integer codes are returned instead of a factor.
include.lowest logical, indicating if an ‘x[i]’ equal to the lowest (or highest, for right = FALSE) ‘breaks’ value should be included.
right logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa.
dig.lab integer which is used when labels are not given. It determines the number of digits used in formatting the break numbers.
ordered_result logical: should the result be an ordered factor?
&#8230; further arguments passed to or from other methods.

Return Value

Sebuah faktor dikembalikan, kecuali label = FALSE yang menghasilkan vektor bilangan bulat dari kode level. Nilai yang berada di luar rentang jeda diberi kode NA, begitu pula nilai NaN dan NA.

Details

Ketika jeda ditentukan sebagai satu angka, rentang data dibagi menjadi potongan-potongan dengan panjang yang sama, dan kemudian batas terluar dipindahkan sebesar 0,1% dari rentang untuk memastikan bahwa nilai ekstrem keduanya berada dalam interval jeda. (Jika x adalah vektor konstan, interval yang sama panjangnya akan dibuat, salah satunya mencakup nilai tunggal.) Jika parameter labels ditentukan, nilainya digunakan untuk memberi nama tingkat faktor. Jika tidak ada yang ditentukan, label tingkat faktor dibuat sebagai "(b1, b2]", "(b2, b3]" dll. untuk kanan = TRUE dan sebagai "[b1, b2)", … jika benar = FALSE. Dalam kasus ini, dig.lab menunjukkan jumlah digit minimum yang harus digunakan dalam memformat angka b1, b2, …. Nilai yang lebih besar (hingga 12) akan digunakan jika diperlukan untuk membedakan antara pasangan titik akhir mana pun: jika ini

Contoh

Example
R
# NOT RUN {
Z <- stats::rnorm(10000)
table(cut(Z, breaks = -6:6))
sum(table(cut(Z, breaks = -6:6, labels = FALSE)))
sum(graphics::hist(Z, breaks = -6:6, plot = FALSE)$counts)

cut(rep(1,5), 4) #-- dummy
tx0 <- c(9, 4, 6, 5, 3, 10, 5, 3, 5)
x <- rep(0:8, tx0)
stopifnot(table(x) == tx0)

table( cut(x, b = 8))
table( cut(x, breaks = 3*(-2:5)))
table( cut(x, breaks = 3*(-2:5), right = FALSE))

##--- some values OUTSIDE the breaks :
table(cx  <- cut(x, breaks = 2*(0:4)))
table(cxl <- cut(x, breaks = 2*(0:4), right = FALSE))
which(is.na(cx));  x[is.na(cx)]  #-- the first 9  values  0
which(is.na(cxl)); x[is.na(cxl)] #-- the last  5  values  8


## Label construction:
y <- stats::rnorm(100)
table(cut(y, breaks = pi/3*(-3:3)))
table(cut(y, breaks = pi/3*(-3:3), dig.lab = 4))

table(cut(y, breaks =  1*(-3:3), dig.lab = 4))
# extra digits don't "harm" here
table(cut(y, breaks =  1*(-3:3), right = FALSE))
#- the same, since no exact INT!

## sometimes the default dig.lab is not enough to be avoid confusion:
aaa <- c(1,2,3,4,5,2,3,4,5,6,7)
cut(aaa, 3)
cut(aaa, 3, dig.lab = 4, ordered = TRUE)

## one way to extract the breakpoints
labs <- levels(cut(aaa, 3))
cbind(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
      upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
# }

See Also

split for splitting a variable according to a group factor; factor tabulate table findInterval. quantile for ways of choosing breaks of roughly equal content (rather than length). .bincode for a bare-bones version.