levels
Get Or Set Levels Attribute
Description
Returns the levels of an object, or
sets the levels attribute for an object.
This is most useful for factors.
This function is implemented as an S3 method.
Usage
levels(x)
levels(x) <- value
Arguments
x |
any object. Typically a factor.
|
value |
a vector of character strings containing the new levels or
a named list of vectors of character strings.
- If value is a vector of character strings, value must have one entry for
each of the current levels of the factor x and the and the i'th
entry in levels(x) is mapped to the i'th entry in value.
After this mapping, duplicate entries and NA entries in value
are deleted, so you can combine levels and omit levels.
- If value is a named list of character vectors, it is expected that those
character vectors contain entries from levels(x) and that there
are no duplicate values among those vectors.
The new levels are names(value) and the entries of levels(x)
in value[[name]] are combined into the new level name.
|
Value
levels(x) returns the levels attribute of x if x has levels.
Otherwise, it returns NULL.
See Also
Examples
FFtypes <- Sdatasets::fuel.frame$Type[seq(1,60,by=7)]
FFtypes
# one way of combining all but "Sporty" into two categories
levels(FFtypes) <- c("Little", "Big", "Big", "Little", NA_character_, "Big")
FFtypes
# another way of doing the same
FFtypes <- Sdatasets::fuel.frame$Type[seq(1,60,by=7)]
levels(FFtypes) <- list(Little = c("Compact", "Small"), Big = c("Large", "Medium", "Van"))
FFtypes