Learn R Programming

varhandle (version 2.0.3)

unfactor: Convert factor into appropriate class

Description

This function gets a factor vector, data.frame or matrix (that contains factor columns), detects the real class of the values and convert factor to the real class.

Usage

unfactor(obj)

Arguments

obj

The factor vector, data.fram or matrix. (Mandatory)

Value

In case of using a vector as an input, a character vector or numeric vector. This depends on the type of values the input variable contains. Check the details section for detailed information. In case of using a data.frame or matrix, the same data.frame/matrix but with converted columns. Note that in data.frame you can have columns with different class but in matrix the should all have one class.

Details

This function turns factors to their real values. If a matrix or data.frame given, it detects factor columns and unfactor them, so you can give the whole data.fram or matrix and the function takes care of the rest. The values' real class detection mechanism is in a way that if everything in that column or vector are numbers and a decimal character, it change it to numeric otherwise it will be changed to character vector.

See Also

as.character, as.numeric

Examples

Run this code
# NOT RUN {
    # load a dataframe (from base package)
    data(iris)
    
    # see the actual values of the categorical column
    class(iris$Species)
    
    # use vactor as input
    species <- unfactor(iris$Species)
    # check the class
    class(species)
    
    # use data.frame as input
    my_iris <- data.frame(Sepal.Length=factor(iris$Sepal.Length), sample_id=factor(1:nrow(iris)))
    my_iris <- unfactor(my_iris)
    # check the class
    class(my_iris)
    class(my_iris$Sepal.Length)
    class(my_iris$sample_id)
# }

Run the code above in your browser using DataLab