codes(object)
object
is not ordered)
of
object
for the corresponding element of
object
.
object
does not inherit from
"factor"
, then
as.vector(object)
is returned.
In the case of an ordered factor, the result has
1
wherever
the first level occurs,
2
wherever the second occurs, etc.
Thus the ordering relation of the levels is preserved.
In the case of an unordered factor, the levels are sorted
and
1
appears wherever the first of the ordered levels occurs,
etc.
This maintains the definition of the levels of a factor as a set;
that is, the behavior of the factor is independent of the order
in which the levels appear in the levels attribute.
Users should not strip the class from a factor and expect correct results.
codes(ordered(c("cat", "dog", "frog", "dog"))) # returns c(1, 2, 3, 2) temperature <- ordered(c(140, 140, 125, 125, 130, 130)) as.vector(temperature, "numeric") # coerce to numeric, returns c(140, 140, 125, 125, 130, 130)