
factorToDumms <- function(x) 
{
   require(dummies)
   tmp <- dummy(x,sep='.')
   if (!any(is.na(x))) return(tmp)
   # need to make NA all dummies corresponding to an NA
   # get name of x
   xname <- as.character(sys.call(1))[2]
   # get name of the NA column
   nacolnm <- paste0(xname,'.NA')
   # which col is it?
   nacol <- which(colnames(tmp) == nacolnm)
   # jettison it
   tmp <- tmp[,-nacol]
   # where are the NAs?
   nas <- which(is.na(x))
   # properly make those rows NAs
   tmp[nas,] <- NA
   tmp
}

