Compared to base R, this function creates levels in the order in which they appear, which will be the same on every platform. (Base R sorts in the current locale which can vary from place to place.)

as_factor(x, ...)

# S3 method for factor
as_factor(x, ...)

# S3 method for character
as_factor(x, ...)

# S3 method for numeric
as_factor(x, ...)

Arguments

x

Object to coerce to a factor.

...

Other arguments passed down to method.

Details

This is a generic function.

Examples

x <- c("a", "z", "g") as_factor(x)
#> [1] a z g #> Levels: a z g
#> [1] a z g #> Levels: a g z
y <- c("1.1", "11", "2.2", "22") as_factor(y)
#> [1] 1.1 11 2.2 22 #> Levels: 1.1 11 2.2 22
#> [1] 1.1 11 2.2 22 #> Levels: 1.1 11 2.2 22