tirsdag den 25. oktober 2016

creating dummy variables

I some times have to create a bunch of dummy variables from a character variable. It is tedious bussiness to create dummy variables one at the time. Some time ago I came accross this blog post dummies-for-dummies  by Alan Fernihough.  The little function he have create have saved me a lot of time.

### start script ###

# Dummy variable function
datdum <- function(x, data, name){
  data$rv <- rnorm(dim(data)[1],1,1)
  mm <- data.frame(model.matrix(lm(data$rv~-1+factor(data[,x]))))
  names(mm) <- paste(name)
  data$rv <- NULL
  data <- cbind(data,mm)
  return(data)
}

# Creating a simple example dataframe "dat" with one column "dat1" consisting of the values "A","B","C".
dat1 <- c("A","B","C")
dat <- data.frame(dat1)

# Using the dummy variable function:
datdum(x="dat1",data=dat, name=dat1)

### end script ###

Maybe it will save you time too. 

Cheers Toke