Faktor fungsi digunakan untuk mengkodekan vektor sebagai faktor (istilah 'kategori' dan 'tipe enumerasi' juga digunakan untuk faktor). Jika argumen yang diurutkan BENAR, level faktor diasumsikan terurut. Untuk kompatibilitas dengan S juga ada fungsi yang dipesan. is.factor, is.ordered, as.factor dan as.ordered adalah fungsi keanggotaan dan paksaan untuk kelas-kelas ini.
factor(x = character(), levels, labels = levels,
exclude = NA, ordered = is.ordered(x), nmax = NA)<p></p><p>ordered(x, …)</p><p>is.factor(x)
is.ordered(x)</p><p>as.factor(x)
as.ordered(x)</p><p>addNA(x, ifany = FALSE)</p>
| Parameter | Deskripsi |
|---|---|
x |
a vector of data, usually taking a small number of distinct values. |
levels |
an optional vector of the unique values (as character strings) that x might have taken. The default is the unique set of values taken by as.character(x), sorted into increasing order of x. Note that this set can be specified as smaller than sort(unique(x)). |
labels |
either an optional character vector of labels for the levels (in the same order as levels after removing those in exclude), or a character string of length 1. Duplicated values in labels can be used to map different values of x to the same factor level. |
exclude |
a vector of values to be excluded when forming the set of levels. This may be factor with the same level set as x or should be a character. |
ordered |
logical flag to determine if the levels should be regarded as ordered (in the order given). |
nmax |
an upper bound on the number of levels; see ‘Details’. |
… |
(in ordered(.)): any of the above, apart from ordered itself. |
ifany |
only add an NA level if it is used, i.e. if any(is.na(x)). |
# NOT RUN {
(ff <- factor(substring("statistics", 1:10, 1:10), levels = letters))
as.integer(ff) # the internal codes
(f. <- factor(ff)) # drops the levels that do not occur
ff[, drop = TRUE] # the same, more transparently
factor(letters[1:20], labels = "letter")
class(ordered(4:1)) # "ordered", inheriting from "factor"
z <- factor(LETTERS[3:1], ordered = TRUE)
## and "relational" methods work:
stopifnot(sort(z)[c(1,3)] == range(z), min(z) < max(z))
# }
# NOT RUN {
## suppose you want "NA" as a level, and to allow missing values.
(x <- factor(c(1, 2, NA), exclude = NULL))
is.na(x)[2] <- TRUE
x # [1] 1 <NA> <NA>
is.na(x)
# [1] FALSE TRUE FALSE
## More rational, since R 3.4.0 :
factor(c(1:2, NA), exclude = "" ) # keeps <NA> , as
factor(c(1:2, NA), exclude = NULL) # always did
## exclude = <character>
z # ordered levels 'A < B < C'
factor(z, exclude = "C") # does exclude
factor(z, exclude = "B") # ditto
## Now, labels maybe duplicated:
## factor() with duplicated labels allowing to "merge levels"
x <- c("Man", "Male", "Man", "Lady", "Female")
## Map from 4 different values to only two levels:
(xf <- factor(x, levels = c("Male", "Man" , "Lady", "Female"),
labels = c("Male", "Male", "Female", "Female")))
#> [1] Male Male Male Female Female
#> Levels: Male Female
## Using addNA()
Month <- airquality$Month
table(addNA(Month))
table(addNA(Month, ifany = TRUE))
# }