factor()
along with cheaper utilitiesA fast version of factor()
using the collapse package.
There are some additional utilities, most of which begin with the prefix
'levels_', such as
as_factor()
which is an efficient way to coerce both vectors and factors,
levels_factor()
which returns the levels of a factor, as a factor,
levels_used()
which returns the used levels of a factor,
levels_unused()
which returns the unused levels of a factor,
levels_add_na()
which adds an explicit NA
level,
levels_drop_na()
which drops the NA
level,
levels_drop()
which drops unused factor levels,
and finally levels_reorder()
which reorders the levels of x
based on y
using the ordered median values of y
for each level.
factor_(
x = integer(),
levels = NULL,
order = TRUE,
na_exclude = TRUE,
ordered = is.ordered(x)
)as_factor(x)
levels_factor(x)
levels_used(x)
levels_unused(x)
used_levels(x)
unused_levels(x)
levels_add_na(x, name = NA, where = c("last", "first"))
levels_drop_na(x)
levels_drop(x)
levels_reorder(x, order_by, decreasing = FALSE)
A factor
or character
in the case of levels_used
and levels_unused
.
A vector.
Optional factor levels.
Should factor levels be sorted? Default is TRUE
.
It typically is faster to set this to FALSE
, in which case the levels
are sorted by order of first appearance.
Should NA
values be excluded from the factor levels?
Default is TRUE
.
Should the result be an ordered factor?
Name of NA
level.
Where should NA
level be placed? Either first or last.
A vector to order the levels of x
by using the medians of
order_by
.
Should the reordered levels be in decreasing order?
Default is FALSE
.
This operates similarly to collapse::qF()
.
The main difference internally is that collapse::funique()
is used
and therefore s3 methods can be written for it.
Furthermore, for date-times factor_
differs in that it differentiates
all instances in time whereas factor
differentiates calendar times.
Using a daylight savings example where the clocks go back:
factor(as.POSIXct(1729984360, tz = "Europe/London") + 3600 *(1:5))
produces 4 levels whereas
factor_(as.POSIXct(1729984360, tz = "Europe/London") + 3600 *(1:5))
produces 5 levels.