mapFactor {gdata} | R Documentation |
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.
mapFactor(x, codes=TRUE, sort=TRUE, drop=FALSE, ...)
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 |
sort
and ...
arguments provides possibility to "order"
levels and can only be used for characters and not for factors.
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.
Gregor Gorjanc
factor
, levels
, unclass
,
attributes
## 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)