Skip to contents

Quickly create a factor

Usage

fact(x)

# S3 method for default
fact(x)

# S3 method for character
fact(x)

# S3 method for numeric
fact(x)

# S3 method for integer
fact(x)

# S3 method for Date
fact(x)

# S3 method for POSIXt
fact(x)

# S3 method for logical
fact(x)

# S3 method for factor
fact(x)

# S3 method for fact
fact(x)

# S3 method for pseudo_id
fact(x)

# S3 method for haven_labelled
fact(x)

Arguments

x

A vector of values

Value

A vector of equal length of x with class fact and factor. If x was ordered, that class is added in between.

Details

fact() can be about 5 times quicker than factor() or as.factor() as it doesn't bother sorting the levels for non-numeric data or have other checks or features. It simply converts a vector to a factor with all unique values as levels with NAs included.

fact.factor() will perform several checks on a factor to include NA levels and to check if the levels should be reordered to conform with the other methods. The fact.fact() method simple returns x.

level orders

The order of the levels may be adjusted to these rules depending on the class of x:

character

The order of appearance

numeric/integer/Date/POSIXt

By the numeric order

logical

As TRUE, FALSE, then NA if present

factor

Numeric if levels can be safely converted, otherwise as they are

See also