Skip to content

Instantly share code, notes, and snippets.

@randyzwitch
Last active January 2, 2016 00:29
Show Gist options
  • Save randyzwitch/8223853 to your computer and use it in GitHub Desktop.
Save randyzwitch/8223853 to your computer and use it in GitHub Desktop.
Create dummy variables to bypass 32-level limit using RandomForests
#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