Coercing character columns into factors
As you saw in the last chapter, one of the features of readr
import functions is that they don't automatically convert strings into factors like read.csv()
does. Sometimes, though, you want one or more columns of data to be interpreted as factors.
In these situations, you can use parse_factor()
after importing your data. You need to supply the column to be parsed and a vector of possible values as arguments to your call. In this case, you'll be parsing the title
and gender
columns of a data frame salaries
(available in your workspace).
This exercise is part of the course
Reading Data into R with readr
Exercise instructions
- Use
parse_factor()
to convert thetitle
column ofsalaries
to a factor. Supplyc("Prof", "AsstProf", "AssocProf")
as thelevels
argument. Assign the result of the function call back tosalaries$title
. - Use the same function to parse the
gender
column ofsalaries
. Supplyc("Male", "Female")
as thelevels
argument. Assign the result tosalaries$gender
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Parse the title column
salaries$title <- ___
# Parse the gender column
salaries$gender <- ___