base::factor() does not let you use duplicated levels nicely. It results in an ugly warning message and you need to use base::droplevels() to get the desired output. The "solution" is to first factor the vector, and then use a named list with the base::levels() function. This function is a wrapper around those steps.

Factor(invec, levels = list(), store = TRUE, ...)

# S3 method for Factor
print(x, ...)

RestoreFactor(invec)

Arguments

invec

A vector that needs to be factored.

levels

A named list of the levels. The name is the level and the values are what should be mapped to those levels.

store

Logical. Should the input values be stored as an attribute?

...

Additional arguments to factor.

x

The object to be printed.

Value

A factored variable with class of factor and Factor, optionally with an attribute of "Input" which stores the original input values.

References

http://stackoverflow.com/a/19410249/1270695

See also

Author

Ananda Mahto

Examples

x <- c("Y", "Y", "Yes", "N", "No", "H") Factor(x, list(Yes = c("Yes", "Y"), No = c("No", "N")))
#> Input values: #> [1] "Y" "Y" "Yes" "N" "No" "H" #> #> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: Yes No
Factor(x, list(Yes = c("Yes", "Y"), No = c("No", "N")), FALSE)
#> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: Yes No
y <- Factor(x, list(No = c("No", "N"), Yes = c("Yes", "Y")), ordered = TRUE) y
#> Input values: #> [1] "Y" "Y" "Yes" "N" "No" "H" #> #> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: No < Yes
RestoreFactor(y)
#> [1] "Y" "Y" "Yes" "N" "No" "H"