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)
invec | A |
---|---|
levels | A named |
store | Logical. Should the input values be stored as an attribute? |
... | Additional arguments to |
x | The object to be printed. |
A factored variable with class
of factor
and Factor
, optionally
with an attribute
of "Input"
which stores the original input values.
http://stackoverflow.com/a/19410249/1270695
Ananda Mahto
#> Input values: #> [1] "Y" "Y" "Yes" "N" "No" "H" #> #> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: Yes No#> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: Yes No#> Input values: #> [1] "Y" "Y" "Yes" "N" "No" "H" #> #> Factored output: #> [1] Yes Yes Yes No No <NA> #> Levels: No < YesRestoreFactor(y)#> [1] "Y" "Y" "Yes" "N" "No" "H"