【R语言学习笔记】10. 将分类变量转换为哑变量


 

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

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM