factor_to_numeric: Convert a factor to integer/numeric

View source: R/factor_to_numeric.R

factor_to_numericR Documentation

Convert a factor to integer/numeric

Description

Different than lamisc::as_numeric_factor(). This function returns corresponding ordinal level value for the factor values. For a factor f, it works like you would think that as.numeric(f) or as.numeric(as.character(f)) would work (both of these give an error). This one is hard to explain but see the example.

Usage

factor_to_numeric(x)

Arguments

x

an object, for example a factor.

Value

A numeric object

Examples

x1 <- c("Dec", "Apr", "Jan", "Mar")

month_levels <- c(
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
)

y1 <- factor(x1, levels = month_levels)
y1

factor_to_numeric(y1)


emilelatour/lamisc documentation built on April 9, 2024, 10:33 a.m.