การดึงข้อมูลเข้าโปรแกรม R ต้องเป็นไฟล์ข้อมูล csv และตั้งชื่อว่า df โดยใช้คำสั่ง read.csv(file.choose())
df = read.csv(file.choose())
head(df)
## Id MSSubClass MSZoning LotFrontage LotArea Street YearBuilt SalePrice
## 1 12 60 RL 85 11924 Pave 2005 345000
## 2 14 20 RL 91 10652 Pave 2006 279500
## 3 21 60 RL 101 14215 Pave 2005 325300
## 4 26 20 RL 110 14230 Pave 2007 256300
## 5 28 20 RL 98 11478 Pave 2007 306000
## 6 33 20 RL 85 11049 Pave 2007 179900
ต้องใช้คำสั่ง is.na() ตรวจสอบ Missing จุดไหนคือ Missing บ้างถ้า FALSE คือ มีข้อมูล ถ้า TRUE คือ ข้อมูลสูญหาย (สร้างข้อมูลตัวอย่าง และตั้งชื่อว่า sample = c( ) ใช้ในการทดสอบคำสั่ง) และใช้ คำสั่ง sum(is.na()) นับจำนวน TRUE ในกรณีที่มีจำนวนข้อมูลเยอะ
sample = c(5,2,NA,3,2,NA,3)
is.na(sample)
## [1] FALSE FALSE TRUE FALSE FALSE TRUE FALSE
sum(is.na(sample))
## [1] 2
นับจำนวน Missing แต่ละ column และ row โดยใช้คำสั่ง colSums(is.na(data)) และ rowSums(is.na(data))
colSums(is.na(df))
## Id MSSubClass MSZoning LotFrontage LotArea Street
## 0 0 0 1 0 0
## YearBuilt SalePrice
## 0 0
rowSums(is.na(df))
## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [38] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [75] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [112] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [149] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [186] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
นับจำนวน column และ row โดยใช้คำสั่ง dim()
dim(df)
## [1] 222 8
นำแถวที่มี missing ออกจากข้อมูลโดยใช้คำสั่ง na.omit()
data = na.omit(df)
dim(df)
## [1] 222 8
เรียกดูชื่อ column
names(df)
## [1] "Id" "MSSubClass" "MSZoning" "LotFrontage" "LotArea"
## [6] "Street" "YearBuilt" "SalePrice"
สร้าง scatter plot เบื้องต้น โดยคำสั่ง plot() ต้องเปิดไฟล์ให้โปรแกรมเห็นชื่อ column ก่อน โดยคำสั่ง attach()
attach(df)
plot( LotFrontage , LotArea )
เปลี่ยนสีของจุดบนกราฟด้วยคำสั่ง col โดยสามารถกำหนดสีได้เองตามที่ต้องการ
plot(LotFrontage,LotArea,
col = "pink")
เปลี่ยนลักษณะของจุดบนกราฟด้วยคำสั่ง pch
plot( LotFrontage, LotArea ,
col = "pink",pch = 18 )
เปลี่ยนขนาดตัวอักษรตรงชื่อของแกน x แกน y โดยใช้คำสั่ง cex.lab
plot( LotFrontage , LotArea ,
col = "pink" , pch = 18 ,
cex.lab = 1.5 )
เปลี่ยนขนาดตัวอักษรตรงตัวเลขในแกน x แกน y โดยคำสั่ง cex.axis
plot( LotFrontage , LotArea ,
col = "pink" , pch = 18 ,
cex.lab = 1.5 , cex.axis = 1 )
ปรับขนาดของจุดบนกราฟ โดยคำสั่ง cex
plot( LotFrontage , LotArea ,
col = "pink" , pch = 18 ,
cex.lab = 1.5 , cex.axis = 1 ,
cex = 0.5 )
เปลี่ยนชื่อของแกน X โดยคำสั่ง xlab และเปลี่ยนชื่อของแกน y โดยคำสั่ง ylab
plot( LotFrontage , LotArea ,
col = "pink" , pch = 18 ,
cex.lab = 1.5 , cex.axis = 1 ,
cex = 0.5 , xlab = "Lot Frontage",
ylab = "Lot Area" )
เริ่มจากการสร้างตารางการแจกแจงความถี่
names(df)
## [1] "Id" "MSSubClass" "MSZoning" "LotFrontage" "LotArea"
## [6] "Street" "YearBuilt" "SalePrice"
table( MSZoning , MSSubClass )
## MSSubClass
## MSZoning 20 60 80 120 160 180
## FV 12 17 0 3 8 0
## RL 95 52 2 28 0 0
## RM 0 0 0 2 0 3
สร้างกราฟแท่งโดยใช้คำสั่ง barplot
barplot(table(MSZoning,MSSubClass), col=c("blue","orange","purple"))
ปรับให้เป็นกราฟแท่งปกติโดยใช้คำสั่ง beside
barplot(table(MSZoning,MSSubClass), col=c("blue","orange","purple") ,
beside = TRUE)
เพิ่มคำอธิบายกราฟโดยใช้คำสั่ง legend.text = TRUE
barplot(table(MSZoning,MSSubClass), col=c("blue","orange","purple") ,
beside = TRUE ,legend.text = TRUE)
เพิ่มคำอธิบายแกน X และแกน Yโดย xlab = “” ylab = “”
barplot(table(MSZoning,MSSubClass), col=c("blue","orange","purple") ,
beside = TRUE , legend.text = TRUE , xlab = "The Building Class")
par(mfrow = c(1,2))
plot( LotFrontage , SalePrice,
col = "pink" , pch = 18 ,
cex.lab = 1.5 , cex.axis = 1 ,
cex = 0.5 )
plot( SalePrice , LotArea,
col = "pink" , pch = 18 ,
cex.lab = 1.5 , cex.axis = 1 ,
cex = 0.5 )
การสร้าง Boxplot แนวตั้ง
boxplot(LotFrontage , col = "brown")
การสร้าง Boxplot แนวนอนโดยกำหนด horizontal = TRUE
boxplot( LotFrontage , col = "brown" ,
horizontal = TRUE )