การดึงไฟล์ข้อมูล

การดึงข้อมูลเข้ามาโดยวิธีเลือกไฟล์จากโฟลเดอร์ โดยใช้ read.csv และตั้งชื่อว่า data

data = read.csv("sample_houseprice.csv")
head(data) #เรียกดู 5 แถวแรกของข้อมูล
##   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

ตรวจสอบ Missing

ใช้คำสั่ง 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

ใช้คำสั่ง sum นับจำนวน TRUE

sum(is.na(sample))
## [1] 2

ใช้คำสั่ง colSums และ rowSums นับจำนวน Missing ในแต่ละคอลัมน์และแถว

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

ใช้คำสั่ง dim() นับจำนวน column และ row

dim(data)
## [1] 222   8

ใช้คำสั่ง na.omit() นำข้อมูลที่ Missing ออก

data = na.omit(data)
dim(data)
## [1] 221   8

ใช้คำสั่ง attach เปิดให้โปรแกรมเห็นชื่อ column

attach(data)

ใช้คำสั่ง names เรียกดูชื่อ column

names(data)
## [1] "Id"          "MSSubClass"  "MSZoning"    "LotFrontage" "LotArea"    
## [6] "Street"      "YearBuilt"   "SalePrice"

##การสร้าง scatter plot เบื้องต้น กำหนดตัวแปร

x = LotFrontage
y = LotArea

ใช้คำสั่ง plot สร้างกราฟ

plot(x,y)

ใช้คำสั่ง main ตั้งชื่อกราฟ

plot(x,y,main="My Plot")

ใช้คำสั่ง col เปลี่ยนสีจุดบนกราฟ

plot(x,y,main="My Plot",col = "red")

ใช้คำสั่ง pch เปลี่ยนลักษณะจุดบนกราฟ

plot(x,y,main="My Plot",col = "red",pch = 16)

ใช้คำสั่ง cex.lab เปลี่ยนขนาดตัวอักษรตรงแกน x และ y

plot(x,y,main="My Plot",col = ,pch = 16,cex = 1.5,cex.lab =1.5)

ใช้คำสั่ง cex.axis เปลี่ยนขนาดตัวเลขตรงแกน x และ y

plot(x,y,main="My Plot",col = "red",pch = 16,cex = 1.5,cex.lab =1.5,cex.axis = 1.5)

ใช้คำสั่ง xlab และ ylab เพื่อเปลี่ยนชื่อบนแกน x และ y

plot(x,y,main="My Plot",col = "red",pch = 16,cex = 1.5,cex.lab =1.5,cex.axis = 1.5, xlab = "Lot frontage",ylab = "Lot area")

##การสร้างกราฟ 2 คอลัมน์ 1 แถวในหน้าเดียวกัน #mfrow = c(จำนวนของแถว,จำนวนของคอลัมน์)

par(mfrow = c(1,2))
plot(LotFrontage,SalePrice,col = "coral",pch = 16)
plot(LotArea,SalePrice,col = "coral",pch = 16)

##การสร้างกราฟ 2 กราฟในภาพเดียวกัน #ข้อมูลตัวอย่าง ข้อมูลชุดแรก, อายุและความเร็วรถยนต์ 12 คัน:

x1 <- c(6,7,8,7,1,2,9,4,13,12,9,7)
y1 <- c(99,86,87,89,112,103,87,94,70,77,85,86)

ข้อมูลชุดที่สอง, อายุและความเร็วรถยนต์ 15 คัน:

x2 <- c(2,2,8,1,14,8,12,9,7,3,12,4,7,14,15)
y2 <- c(100,106,84,105,91,99,90,95,94,100,79,115,91,80,85)
plot(x1, y1, main="Observation of Cars", xlab="Car age", ylab="Car speed", col="pink", cex=2)
points(x2, y2, col="#6699CC", cex=2)

##การสร้างกราฟแท่ง หรือ Bar Charts เบื้องต้น เริ่มจากการสร้างตารางการแจกแจงความถี่

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","yellow"))

ใช้คำสั่ง density เปลี่ยนพื้นผิวของกราฟแท่ง

barplot(table(MSZoning,MSSubClass),col=c ("red","green","yellow") ,density = 10)

ใช้คำสั่ง horiz=TRUE หากต้องการให้กราฟแท่งแสดงในแนวนอนแทนที่จะเป็นแนวตั้ง

barplot(table(MSZoning,MSSubClass),col=c ("red","green","yellow")  , horiz = TRUE)

ใช้คำสั่ง beside เพื่อปรับให้เป็นกราฟแท่งปกติ

barplot(table(MSZoning,MSSubClass),col=c ("red","green","yellow"), beside = TRUE)

เพิ่มคำอธิบายกราฟ

barplot(table(MSZoning,MSSubClass),col=c ("red","green","yellow"), beside = TRUE,legend.text = TRUE)

เพิ่มคำอธิบายแกน X แกน Y

barplot(table(MSZoning,MSSubClass),col=c ("red","green","yellow") , beside = TRUE,legend.text = TRUE, xlab = "The building class")

##การสร้างกราฟเส้น หรือ Line Graphs เบื้องต้น ใช้คำสั่ง plot และ type=“l” เพื่อสร้างกราฟเส้น

plot(1:10, type="l")

ใช้คำสั่ง col เปลี่ยนสีของเส้นบนกราฟ

plot(1:10, type="l",col= "pink")

ใช้คำสั่ง lwd เปลี่ยนขนาดของเส้นบนกราฟ (โดยที่ 1 เป็นค่าเริ่มต้น 0.5 หมายถึงเล็กลง 50% และ 2 หมายถึงใหญ่ขึ้น 100%)

plot(1:10, type="l",col = "pink", lwd=2)

ใช้คำสั่ง tly เปลี่ยนลักษณะของเส้นบนกราฟ โดยที่ คำสั่งที่พร้อมใช้งานสําหรับ lty :

• 0 ลบบรรทัด

• 1 แสดงเส้นทึบ

• 2 แสดงเส้นประ

• 3 แสดงจุดไข่ปลา

• 4 แสดงเส้น “dot dashed”

• 5 แสดงเส้น “เส้นประยาว”

• 6 แสดงเส้น “สองเส้นประ”

plot(1:10, type="l",col = "pink", lwd=5,lty=3)

##การสร้าง Boxplot สร้าง boxplot แนวตั้ง

boxplot(LotFrontage,col = "pink")

สร้าง boxplot แนวนอน โดยการกำหนด horizontal = TRUE

boxplot(LotFrontage,col = "#FFB6B7",horizontal = TRUE)