ordered
which is an ordered category.
ordered(x, levels=<<see below>>, labels=<<see below>>, exclude=NA) ordered(x) <- levels is.ordered(x) as.ordered(x)
levels
is
coded in the output vector as
NA
.
The levels will be assumed ordered (low to high) in the order given.
If omitted, the sorted unique values of
x
will be used.
Note that the sort function removes numeric NA's before
sorting, so the default
levels
for numeric data will not include
any NAs. If
x
is character data or you wish to exclude other
values from the
levels
you may use the
exclude
argument.
x
and
exclude
will be
NA
in the
result and it will not appear in the default
levels
attribute.
ordered
returns an ordered factor, i.e.,
an object of class
c("ordered", "factor")
.
is.ordered
returns
TRUE
if
x
inherits from class
"ordered"
, and
FALSE
otherwise.
as.ordered
returns
x
if
x
inherits from class
"ordered"
, and returns
ordered(x)
otherwise.
ordered
is used on the left of an assignment, the levels of
x
will be taken to be ordered according to the argument on the right
side of the assignment.
Typically,
levels
in this case will consist of some permutation of the
current levels of
x
.
If values in
levels(x)
are missing from
levels
, any corresponding
data values in
x
will become
NA
.
The assignment can also be applied to a data frame, in which case the
right side is taken to apply to each of the variables in the data frame.
The right side should be either a logical vector of length equal
to the number of variables, or else a list of the same length.
In the case of a list, each element acts like the right side of
an assignment of the ordered attribute of the corresponding variable.
The assignment version of the function,
"ordered<-"
is generic.
There is a method for data frames as well as a default method.
ratings.text <- sample(c("Low","Med","High"), 20, replace=T) ratings <- ordered(ratings.text, c("Low","Med","High") ) # reverse the ordering ordered(ratings) <- c("High","Med","Low") temperature <- ordered(c(140, 140, 125, 125, 130, 130)) as.vector(temperature, "numeric") # coerce to numeric codes(temperature) # numeric vector of indices in to the levels