icular, ` as.numeric ` applied to a factor is
meaningless, and may happen by implicit coercion. To transform a factor ` f `
to approximately its original numeric values, ` as.numeric(levels(f))[f] ` is
recommended and slightly more efficient than ` as.numeric(as.character(f)) ` .

The levels of a factor are by default sorted, but the sort order may well
depend on the locale at the time of creation, and should not be assumed to be
ASCII.

There are some anomalies associated with factors that have ` NA ` as a level.
It is suggested to use them sparingly, e.g., only for tabulation purposes.

##  Comparison operators and group generic methods

There are ` "factor" ` and ` "ordered" ` methods for the [ group generic
](/link/group%20generic?package=base&version=3.6.2) ` [ Ops
](/link/Ops?package=base&version=3.6.2&to=%3DS3groupGeneric) ` which provide
methods for the [ Comparison ](/link/Comparison?package=base&version=3.6.2)
operators, and for the ` [ min ](/link/min?package=base&version=3.6.2) ` , ` [
max ](/link/max?package=base&version=3.6.2) ` , and ` [ range
](/link/range?package=base&version=3.6.2) ` generics in ` [ Summary
](/link/Summary?package=base&version=3.6.2&to=%3DS3groupGeneric) ` of `
"ordered" ` . (The rest of the groups and the ` [ Math
](/link/Math?package=base&version=3.6.2&to=%3DS3groupGeneric) ` group generate
an error as they are not meaningful for factors.)

Only ` == ` and ` != ` can be used for factors: a factor can only be compared
to another factor with an identical set of levels (not necessarily in the same
ordering) or to a character vector. Ordered factors are compared in the same
way, but the general dispatch mechanism precludes comparing ordered and
unordered factors.

All the comparison operators are available for ordered factors. Collation is
done by the levels of the operands: if both operands are ordered factors they
must have the same level set.

##  Details

The type of the vector ` x ` is not restricted; it only must have an ` [
as.character ](/link/as.character?package=base&version=3.6.2) ` method and be
sortable (by ` [ order ](/link/order?package=base&version=3.6.2) ` ).

Ordered factors differ from factors only in their class, but methods and the
model-fitting functions treat the two classes quite differently.

The encoding of the vector happens as follows. First all the values in `
exclude ` are removed from ` levels ` . If ` x[i] ` equals ` levels[j] ` ,
then the ` i ` -th element of the result is ` j ` . If no match is found for `
x[i] ` in ` levels ` (which will happen for excluded values) then the ` i `
-th element of the result is set to ` [ NA
](/link/NA?package=base&version=3.6.2) ` .

Normally the ‘levels’ used as an attribute of the result are the reduced set
of levels after removing those in ` exclude ` , but this can be altered by
supplying ` labels ` . This should either be a set of new labels for the
levels, or a character string, in which case the levels are that character
string with a sequence number appended.

` factor(x, exclude = NULL) ` applied to a factor without ` [ NA
](/link/NA?package=base&version=3.6.2) ` s is a no-operation unless there are
unused levels: in that case, a factor with the reduced level set is returned.
If ` exclude ` is used, since  R  version 3.4.0, excluding non-existing
character levels is equivalent to excluding nothing, and when ` exclude ` is a
` [ character ](/link/character?package=base&version=3.6.2) ` vector, that
_is_ applied to the levels of ` x ` . Alternatively, ` exclude ` can be factor
with the same level set as ` x ` and will exclude the levels present in `
exclude ` .

The codes of a factor may contain ` [ NA
](/link/NA?package=base&version=3.6.2) ` . For a numeric ` x ` , set ` exclude
= NULL ` to make ` [ NA ](/link/NA?package=base&version=3.6.2) ` an extra
level (prints as ` <NA> ` ); by default, this is the last level.

If ` NA ` is a level, the way to set a code to be missing (as opposed to the
code of the missing level) is