In python, scikit has a great function called LabelEncoder that maps categorical levels (strings) to integer representation.
Is there anything in R to do this?
It's hard to believe why no one has mentioned caret
's dummyVars
function.
This is a widely searched question, and people don't want to write their own methods or copy and paste other users methods, they want a package, and caret
is the closest thing to sklearn
in R.
EDIT: I now realize that what the user actually want's is to turn strings into a counting number, which is just as.numeric(as.factor(x))
but I'm going to leave this here because using hot-one encoding is the more accurate method of encoding categorical data.