I want to convert a character vector in R to a factor (let's take the example from the DataCamp Introduction to R course) and would like to label a few of the factor levels. How do I avoid, that any unmentioned/undeclared levels are automatically put to NA?
speed_vector <- c("fast", "slow", "slow", "fast", "insane")
factor_speed_vector <- factor(speed_vector, ordered = TRUE, levels = c("slow", "insane"), labels = c("Speed < 30 mph", "Speed > 100 mph"))
results in
> summary(factor_speed_vector)
Speed < 30 mph Speed > 100 mph NA's
2 1 2
> factor_speed_vector
[1] <NA> Speed < 30 mph Speed < 30 mph <NA> Speed > 100 mph
Levels: Speed < 30 mph < Speed > 100 mph
How can I make sure that any undefined factor level (like "fast" in this example) gets carried over with the original value instead of being set to NA?
Edit: My previous comment here, was due to a confusion of the level and labels option in the factor function. Anybody, also not knowing the difference can read up here: Confusion between factor levels and factor labels