案例:通过分析上海的二手房的数据,分析出性价比(地段,价格,未来的升值空间)来判断哪个区位的二手房性价比最高
1.载入包
library(ggplot2)
library(Hmisc)
library(car)
library(caret)
2.加载数据集
houses <- read.csv('E:\\Udacity\\Data Analysis High\\R\\R_Study\\二手房分析案例\\链家二手房.csv',sep=',',header=T)
3.查看数据集
describe(houses)
数据集有以下几个字段构成
## 小区名称 ## 户型 ## 面积 ## 区域 ## 楼层 ## 朝向 ## 价格.W. ## 单价.平方米. ## 建筑时间
探究影响房价的主要因素是什么
4.查看户型的分布
type_freq <- data.frame(table(houses$户型)) type_p <- ggplot(data=type_freq,aes(x=reorder(Var1,-Freq),y=Freq))+ geom_bar(stat='identity',fill='steelblue')+ theme(axis.text.x = element_text(angle = 30,vjust = 0.5))+ xlab('户型')+ ylab('套数') type_p
结论:户型的分布不符合正态分布
需要对户型的数据进行清洗,找出主要的户型
5.对户型数据进行清洗
# 把低于一千套的房型设置为其他 type <- c('2室2厅','2室1厅','3室2厅','1室1厅','3室1厅','4室2厅','1室0厅','2室0厅','4室1厅') houses$type.new <- ifelse(houses$户型 %in% type,as.character(houses$户型),'其他') type_freq <- data.frame(table(houses$type.new)) # 绘图 type_p <- ggplot(data = type_freq, mapping = aes(x = reorder(Var1, -Freq),y = Freq)) + geom_bar(stat = 'identity', fill = 'steelblue') + theme(axis.text.x = element_text(angle = 30, vjust = 0.5)) + xlab('户型') + ylab('套数') type_p