thanhleviet
5/17/2016 - 10:12 AM

Just for fun

Just for fun

#version 0.0002
rm(list = ls())
#Prepare a fake data 
co.v <- c(13,20,30) #co vector
co <- replicate(100, sample(co.v,1)) #replicate 100 obs of co 
eq <- replicate(100, sample(c("bad", "med", "good"),1)) #replicate 100 obs of eq
dt <- data.frame(co=co,eq_=eq) #Create data frame
dt <- as.data.frame(model.matrix(~ co+eq, data = dt))
dt <- dt[,-1]
names(dt) <- c("co","good", "med")

######################################################################
#dt is the data frame you have with 3 columns: co, good, med 
#Add new column: qualitative co. 
co.d <- c("base","small","large") #qualitative co vector, same order as the numeric co vector (i.e 15 <-> base, 20 <-> small, so on)
dt$co.d <- sapply(dt$co, function(x) co.d[grep(x,co.v)]) #transform numeric co to qualitative co. This function looks complicated but it is very efficient in terms of the computing speed


#Generate dummy variables from eq and co.d using function model.matrix
tmp.dt <- as.data.frame(model.matrix(~ co.d, data = dt))
dt <- data.frame(dt[,c(1:3)], tmp.dt[,c(2,3)])

#Rename temp dt for being convenient
names(dt) <- c("co","good","med","large","small")

#Using mutate function from dplyr package for being convenient
library(dplyr)

#Generate other dummy variables
#Voilà, we have the new final data as expected smile emoticon
final_dt <- dt %>% mutate(small_med = small*med, small_good = small*good, large_med = large*med, large_good = large*good)