การดึงไฟล์ข้อมูล csv เข้ามาในโปรแกรม R และตั้งชื่อว่า data โดยใช่คำสั่ง read.csv()
data = read.csv("sample_houseprice.csv")
head(data)
## 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 ใช้ในการทดสอบคำสั่ง)
sample = c(2,3,5,NA,4,5,NA)
is.na(sample)
## [1] FALSE FALSE FALSE TRUE FALSE FALSE TRUE
นับจำนวน Missing แต่ละ column และ Row
colSums(is.na(data))
## Id MSSubClass MSZoning LotFrontage LotArea Street
## 0 0 0 1 0 0
## YearBuilt SalePrice
## 0 0
rowSums(is.na(data))
## [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(data)
## [1] 222 8
นำแถวที่มี Missing ออกจากข้อมูล
data = na.omit(data)
dim(data)
## [1] 221 8
คำสั่งเปิดให้โปรแกรมเห็นชื่อ column
attach(data)
คำสั่งสร้างกราฟplot
plot(LotFrontage,LotArea)
เปลี่ยนสีของจุดบนกราฟด้วยคำสั่ง col
plot(LotFrontage,LotArea,
col="darkgreen")
เปลี่ยนลักษณะของจุดบนกราฟด้วยคำสั่ง pch
plot(LotFrontage,LotArea,
col = "darkgreen",pch = 16)
เปลี่ยนขนาดตัวอักษรชื่อของแกน x แกน y ด้วยคำสั่ง cex.lab
plot(LotFrontage,LotArea,col = "darkgreen",pch = 16,
cex.lab = 1.5)
เปลี่ยนขนาดตัวอักษรตรงตัวเลขในแกน x แกน y ด้วยคำสั่งcex.axis
plot(LotFrontage,LotArea,col ="darkgreen",pch = 16,
cex.lab = 1.5,
cex.axis = 1.2)
ปรับขนาดของ จุด ด้วยคำสั่ง cex
plot(LotFrontage,LotArea,col = "darkgreen",pch = 16,
cex.lab = 1.5,
cex.axis = 1.2,
cex = 0.8)
เปลี่ยนชื่อของแกน X โดย xlab เปลี่ยนชื่อของแกน y โดย ylab
plot(LotFrontage,LotArea,col = "darkgreen",pch = 16,
cex.lab = 1.5,
cex.axis = 1.2,
cex = 0.8,
xlab = "Lot frontage",
ylab = "Lot area")
เริ่มจากการสร้างตารางการแจกแจงความถี่
names(data)
## [1] "Id" "MSSubClass" "MSZoning" "LotFrontage" "LotArea"
## [6] "Street" "YearBuilt" "SalePrice"
table(MSZoning,MSSubClass)
## MSSubClass
## MSZoning 20 60 80 120 160 180
## FV 11 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 ("red","green","blue"))
ปรับให้เป็นกราฟแท่งปกติโดยใช้คำสั่ง beside
barplot(table(MSZoning,MSSubClass),col=c ("red","green","blue"), beside = TRUE)
เพิ่มคำอธิบายกราฟ
barplot(table(MSZoning,MSSubClass),col=c ("red","green","blue"), beside = TRUE,
legend.text = TRUE)
เพิ่มคำอธิบายแกน X แกน Y
barplot(table(MSZoning,MSSubClass),col=c ("red","green","blue"), beside = TRUE,
legend.text = TRUE,
xlab = "The building class")
par(mfrow = c(1,2))
plot(LotFrontage,SalePrice,col = "coral",pch = 16)
plot(LotArea,SalePrice,col = "coral",pch = 16)
boxplot แนวตั้ง
boxplot(LotFrontage,col = "darkblue")
boxplot แนวนอน โดยการกำหนด horizontal = TRUE
boxplot(LotFrontage,col = "darkblue",
horizontal = TRUE)
สร้างชุดข้อมูลผ่านคำสั่ง data.frame
d = data.frame(menu = c("coffee","tea","water","coke"),
sales = c(40,20,60,25))
d
## menu sales
## 1 coffee 40
## 2 tea 20
## 3 water 60
## 4 coke 25
ใช้คำสั่ง pie สร้างกราฟวงกลม
pie(d$sales)
ใช้คำสั่ง labels เพื่อใส่ชื่อให้แต่ละกราฟ และใช้คำสั่ง clockwise = T เพื่อให้กราฟแสดงข้อมูลตามเข็มนาฬิกา
pie(d$sales, labels = d$menu)
pie(d$sales, labels = d$menu, clockwise = T)
ถ้าอยากให้กราฟแสดงเปอร์เซ็นให้นำค่าของ sales มาหาค่าของเปอร์เซ็นและทำการเพิ่มข้อมูลเปอร์เซ็นในตาราง
d$sales / sum(d$sales) * 100
## [1] 27.58621 13.79310 41.37931 17.24138
d$per = sprintf("%s (%.2f%%)", d$menu, d$sales / sum(d$sales) * 100)
View(d)
pie(d$sales, labels = d$per, clockwise = T, cex=.9)
ใช้คำสั่ง col เพื่อเปลี่ยนสีของกราฟ
pie(d$sales, labels = d$per, clockwise = T, cex=.9, col=c("tan","lightgreen","lightblue","brown"))