Factor: Factor Vectors with Multiple Levels

Description Usage Arguments Value Author(s) References See Also Examples

View source: R/Factor.R

Description

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.

Usage

1
2
3
4
5
6
Factor(invec, levels = list(), store = TRUE, ...)

## S3 method for class '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.

Author(s)

Ananda Mahto

References

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

See Also

base::factor(), base::levels()

Examples

1
2
3
4
5
6
7
x <- c("Y", "Y", "Yes", "N", "No", "H")
Factor(x, list(Yes = c("Yes", "Y"), No = c("No", "N")))
Factor(x, list(Yes = c("Yes", "Y"), No = c("No", "N")), FALSE)
y <- Factor(x, list(No = c("No", "N"), Yes = c("Yes", "Y")), ordered = TRUE)
y

RestoreFactor(y)

mrdwab/SOfun documentation built on June 20, 2020, 6:15 p.m.