setwd("/Users/shanshantong/Desktop/Skills/1.Tools/R/6.Datasets/") housing.df <- read.csv("WestRoxbury.csv", header = TRUE) # load data
table(housing.df$REMODEL)
Method1
# Option1: use dummies package library(dummies) #load dummies package housing.df <- dummy.data.frame(housing.df, sep = ".") #dummy.data.frame() -> create dummy variables for "Remodel" #sep = "." -> separator b/w previous name and level names(housing.df)
Method2
# Option 2: use model.matrix() to convert all categorical variables in the data frame into a set of dummy variables. # We must then turn the resulting data matrix back into a data frame for further work. housing.df <- read.csv("WestRoxbury.csv", header = TRUE) # load data xtotal <- model.matrix(~ 0 + REMODEL, data = housing.df) xtotal <- as.data.frame(xtotal) t(t(names(xtotal))) # check the names of the dummy variables