Levels Attribute

DESCRIPTION:
Returns the levels attribute of an object. This attribute is used for factors and categories.

USAGE:
levels(x)
levels(x) <- value

REQUIRED ARGUMENTS:
x:
any object, typically a category.

OPTIONAL ARGUMENTS:
value:
vector of character strings. The length of value should be max(x).

VALUE:
if x has a levels attribute then the value of this attribute is returned; otherwise NULL.

SIDE EFFECTS:
if an assignment is made to the levels of an object, then the levels (labels) are changed. To explicitly delete the levels attribute, use levels(x) <- NULL.

DETAILS:
The assignment version of this function is generic, with a method for objects of class "factor". This method makes sure that the length of value is the same as the length of the (old) levels of x, but you may combine categories by giving them the same level.

If value is a list and x is a factor, the method returns merge.levels(x, value). See merge.levels for details on how this may be used to combine or add levels to a factor.


SEE ALSO:
factor , ordered , category , table , tapply .

EXAMPLES:
levels(x)[levels(x)=="c"] <- "copper"

levels(fac) <- c("A", "B", "A") # combine the first and third levels of a factor levels(fac) <- list(A=c("One","Three"), B="Two") # combine levels "One" and "Three" into "A" and rename "Two" "B".