Codes of an Ordered Factor

DESCRIPTION:
Returns a numeric vector which is the codes used by an ordered factor. If an unordered factor is given, the codes for the sorted levels of the factor are returned.

USAGE:
codes(object)

REQUIRED ARGUMENTS:
object:
an ordered factor, or a factor.

VALUE:
an integer vector; each element of the result is the index into the levels (the sorted levels if object is not ordered) of object for the corresponding element of object.

If object does not inherit from "factor", then as.vector(object) is returned.


DETAILS:
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.


SEE ALSO:
ordered , factor , levels .

EXAMPLES:
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)