I am trying to compare two factors within a dataframe to create a new variable. The factors have different levels, which is throwing an error.
Here is a reproducible example
library(dplyr)
library(forcats)
mtcars %>%
select(gear, carb) %>%
mutate_at(c("gear", "carb"), ~as_factor(.)) %>%
mutate(gear_vs_carb = gear == carb)
And here is the error:
Error in Ops.factor(gear, carb) : level sets of factors are different
I understand that I can make the comparison by converting the factors to characters or numeric and/or by adding unused levels to the factors to make the levels match, e.g. How can I compare two factors with different levels?
But is it possible to make the comparison directly with the original factors?
The output should look the same as for
mtcars %>%
select(gear, carb) %>%
mutate(gear_vs_carb = gear == carb)
Thank you for your help!