factor_ {cheapr}R Documentation

A faster version of factor()

Description

A fast version of factor() using the collapse package. There are some additional utilities such as levels_factor() which returns the levels of a factor, as a factor, used_levels() which returns the used levels of a factor, and unused_levels() which returns the unused levels of a factor.

Usage

factor_(
  x = integer(),
  levels = NULL,
  order = TRUE,
  na_exclude = TRUE,
  ordered = is.ordered(x)
)

levels_factor(x)

used_levels(x)

unused_levels(x)

Arguments

x

A vector.

levels

Optional factor levels.

order

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.

na_exclude

Should NA values be excluded from the factor levels? Default is TRUE.

ordered

Should the result be an ordered factor?

Details

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.

Value

A factor or character in the case of used_levels and unused_levels.


[Package cheapr version 0.9.3 Index]