The function factor is used to encode a vector as a factor.
Usage
factor(
x = character(),
levels,
labels = levels,
exclude = NA,
ordered = is.ordered(x),
nmax = NA
)
ordered(x = character(), ...)Arguments
- 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
xmight have taken. The default is the unique set of values taken byas.character(x), sorted into increasing order ofx. Note that this set can be specified as smaller thansort(unique(x)).- labels
either an optional character vector of labels for the levels (in the same order as
levelsafter removing those inexclude), or a character string of length 1. Duplicated values inlabelscan be used to map different values ofxto 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
xor should be acharacter.- 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.
- ...
(in
ordered(.)): any of the above, apart fromordereditself.
Examples
x <- as.integer64(c(132724613L, -2143220989L, -1L, NA, 1L))
factor(x)
#> [1] 132724613 -2143220989 -1 <NA> 1
#> Levels: -2143220989 -1 1 132724613
ordered(x)
#> [1] 132724613 -2143220989 -1 <NA> 1
#> Levels: -2143220989 < -1 < 1 < 132724613