Learn R Programming

gdata (version 2.2.0)

mapFactor: Get a map of levels in a factor

Description

mapFactor produces a list with information on levels and internal integer codes. As such can be conveniently used to store factor map when one needs to work with internal codes of a factor and later transfrorm back to factor.

Usage

mapFactor(x, codes=TRUE, sort=TRUE, drop=FALSE, ...)

Arguments

x
factor, the object to be mapped
codes
boolean, create map with internal codes or with levels, look into value and examples
sort
boolean, sort levels for a character, look into details
drop
boolean, drop unused levels of a factor
...
additional arguments for sort

Value

  • A list with names equal to levels and entries equal to internal codes, when codes=TRUE, or entries equal to levels otherwise. The later case is usefull, when one would like to combine two factors with different levels.

Details

sort and ... arguments provides possibility to "order" levels and can only be used for characters and not for factors.

See Also

factor, levels, unclass, attributes

Examples

Run this code
## Example with codes=TRUE
(f <- factor(letters[c(1, 1, 2, 3, 4, 5, 7, 8, 9, 8, 8, 10)]))
map <- mapFactor(f)
int <- as.integer(f)
fNew <- factor(int)
levels(fNew) <- map
fNew

## Example with codes=FALSE
f1 <- factor(f[1:5])
f2 <- factor(f[5:length(f)])
map1 <- mapFactor(f1, codes=FALSE)
map2 <- mapFactor(f2, codes=FALSE)
map <- c(map1, map2)
levels(f1) <- map
levels(f2) <- map
as.integer(f1)
as.integer(f2)

## x <- unique(map)
## names(x) <- unlist(x)

Run the code above in your browser using DataLab