Last active
January 2, 2016 00:29
-
-
Save randyzwitch/8223853 to your computer and use it in GitHub Desktop.
Create dummy variables to bypass 32-level limit using RandomForests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Generate example dataframe with character column | |
example <- as.data.frame(c("A", "A", "B", "F", "C", "G", "C", "D", "E", "F")) | |
names(example) <- "strcol" | |
#For every unique value in the string column, create a new 1/0 column | |
#This is what Factors do "under-the-hood" automatically when passed to function requiring numeric data | |
for(level in unique(example$strcol)){ | |
example[paste("dummy", level, sep = "_")] <- ifelse(example$strcol == level, 1, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment