1.6 Convert number to factor

When variables have numerical levels it is necessary to ensure that the program knows it is a factor variable.

The following code uses the factor() function to take the marital status variable and convert it into a factor variable with specified labels that match the codebook.

depress$marital <- factor(depress$marital, 
      labels = c("Never Married", "Married", "Divorced", "Separated", "Widowed"))

It is important to confirm the recode worked. If it did not you will have to re-read in the raw data set again since the variable marital was replaced.

## Never Married       Married      Divorced     Separated       Widowed 
##            73           127            43            13            38
## [1] "factor"

See more examples on Math 130 Lesson 06