Suppose I've read in a data frame, where a column contains strings as factors. I would like to convert the factors to numerics but with specific mappings. This conversion is typically a precursor step for a later calculation. For example:
> library(rpart)
> head(car90["Type"])
Type
Acura Integra Small
Acura Legend Medium
Audi 100 Medium
Audi 80 Compact
BMW 325i Compact
BMW 535i Medium
> summary(car90$Type)
Compact Large Medium Small Sporty Van NA's
19 7 26 22 21 10 6
In the car90$Type column, I would like to set 'Compact' to be -10, 'Large' to be -1, 'Medium' to be 0, 'Small' to be 1, 'Sporty' to be 10, and 'Van' to be 20, where the numbers are numerics, not factors. How would I do that?
I have already looked at related questions, but none provided a solution.
Replace specific column "words" into number or blank
Changing column names of a data frame in R