Skip to main content

Posts

Showing posts from April, 2017

Creating dummy variables in R

Randy Zwitch has a blog entry on creation of dummy variables from factor levels.

example<- span="">as.data.frame(c("A","A","B","F","C","G","C","D","E","F"))names(example)<- span="">"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(levelinunique(example$strcol)){example[paste("dummy",level,sep="_")]<- span="">ifelse(example$strcol==level,1,0)}viewraw Often you encounter special characters in which case you can use gsub and regular expressions
example<- span="">as.data.frame(c("AÆ","AÆ","B","FÅ","C","G","C","D",