Get startedGet started for free

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

View Course

Exercise instructions

  • Use parse_factor() to convert the title column of salaries to a factor. Supply c("Prof", "AsstProf", "AssocProf") as the levels argument. Assign the result of the function call back to salaries$title.
  • Use the same function to parse the gender column of salaries. Supply c("Male", "Female") as the levels argument. Assign the result to salaries$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 <- ___
Edit and Run Code