Skip to contents

Coerce all columns in the object from factor.

Usage

unfactorize(object, ...)

# S4 method for factor
unfactorize(object)

# S4 method for DFrame
unfactorize(object, j = NULL)

# S4 method for data.frame
unfactorize(object, j = NULL)

Arguments

object

Object.

j

vector. Column names or positions to evaluate.

...

Additional arguments.

Value

Modified object, with factor coerced back to atomic vector.

Note

Updated 2023-09-20.

See also

  • Warning section of help("factor").

  • R FAQ 7.10: How do I convert factors to numeric?

  • stringsAsFactors for read.table and data.frame.

  • https://stackoverflow.com/questions/3418128/

  • forcats package.

Examples

## factor ====
object <- as.factor(c(100L, 100L, 101L, 101L))
## Coercion with `as.numeric` or `as.integer` don't work the way we want.
print(as.numeric(object))
#> [1] 1 1 2 2
print(as.integer(object))
#> [1] 1 1 2 2
object <- factorize(object)
print(object)
#> [1] 100 100 101 101
#> Levels: 100 101

## DFrame ====
object <- S4Vectors::DataFrame(
    "a" = as.factor(c(100L, 100L, 101L, 101L)),
    "b" = as.factor(c("a", "b", "a", "b"))
)
print(object)
#> DataFrame with 4 rows and 2 columns
#>          a        b
#>   <factor> <factor>
#> 1      100        a
#> 2      100        b
#> 3      101        a
#> 4      101        b
object <- unfactorize(object)
print(object)
#> DataFrame with 4 rows and 2 columns
#>           a           b
#>   <integer> <character>
#> 1       100           a
#> 2       100           b
#> 3       101           a
#> 4       101           b