Converting Factors to Numeric Values
At times you require to explicitly change factors to either numbers or text. To achieve this, one has to use the functions as.character() or as.numeric(). There are two steps for converting factor to numeric:
Step 1: Convert the data vector into a factor. The factor() command is used to create and modify factors in R.
Step 2: The factor is converted into a numeric vector using as.numeric().
When a factor is converted into a numeric vector, the numeric codes corresponding to the factor levels will be returned.
However, if you simply use as.numeric(), the output is a vector of the internal level representations of the factor and not the original values.