ID EN
Miscellaneous

levels

R Base 3.6.2 🇮🇩 Bahasa Indonesia

level menyediakan akses ke atribut level suatu variabel. Bentuk pertama mengembalikan nilai level argumennya dan bentuk kedua menetapkan atribut.

Syntax

R
levels(x)
levels(x) <- value

Arguments

Parameter Deskripsi
x an object, for example a factor.
value A valid value for levels(x). For the default method, NULL or a character vector. For the factor method, a vector of character strings with length at least the number of levels of x, or a named list specifying how to rename the levels.

Details

Formulir ekstraktor dan penggantinya bersifat umum dan metode baru dapat ditulis untuk keduanya. Metode penggantian fungsi yang paling penting adalah dengan faktor. Untuk metode penggantian faktor, nilai NA menyebabkan level tersebut dihilangkan dari level tersebut dan elemen yang sebelumnya memiliki level tersebut digantikan oleh NA. Perhatikan bahwa untuk suatu faktor, mengganti level melalui level(x) <- value tidak sama dengan (dan lebih disukai) attr(x, "levels") <- value. Fungsi penggantiannya primitif.

Contoh

Example
R
# NOT RUN {
## assign individual levels
x <- gl(2, 4, 8)
levels(x)[1] <- "low"
levels(x)[2] <- "high"
x

## or as a group
y <- gl(2, 4, 8)
levels(y) <- c("low", "high")
y

## combine some levels
z <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))
z
levels(z) <- c("fruit", "veg", "fruit")
z

## same, using a named list
z <- gl(3, 2, 12, labels = c("apple", "salad", "orange"))
z
levels(z) <- list("fruit" = c("apple","orange"),
                  "veg"   = "salad")
z

## we can add levels this way:
f <- factor(c("a","b"))
levels(f) <- c("c", "a", "b")
f

f <- factor(c("a","b"))
levels(f) <- list(C = "C", A = "a", B = "b")
f
# }

See Also

nlevels relevel reorder.