# 下載加密資料
fL <- paste0("http://", IDPW, "140.116.183.121/~sheu/dataM/Data/juniorSchools.txt")

dta <- read.table(fL, header = T)
# 檢視資料
head(dta)
##   school class sex soc ravens pupil english math year
## 1     S1    C1   G   9     23    P1      72   23    0
## 2     S1    C1   G   9     23    P1      80   24    1
## 3     S1    C1   G   9     23    P1      39   23    2
## 4     S1    C1   B   2     15    P2       7   14    0
## 5     S1    C1   B   2     15    P2      17   11    1
## 6     S1    C1   B   2     22    P3      88   36    0

EX3-1

# 將性別變項重新命名
names(dta)[3] <- "Gender"; head(dta)
##   school class Gender soc ravens pupil english math year
## 1     S1    C1      G   9     23    P1      72   23    0
## 2     S1    C1      G   9     23    P1      80   24    1
## 3     S1    C1      G   9     23    P1      39   23    2
## 4     S1    C1      B   2     15    P2       7   14    0
## 5     S1    C1      B   2     15    P2      17   11    1
## 6     S1    C1      B   2     22    P3      88   36    0

EX3-2

# 以soc變項作為基準,重新進行factor排序
dta$socII <- factor(dta$soc, 1:9, c("I", "II", "III_0man", "III_man", "IV", "V", "VI_Unemp_L", "VII_emp_NC", "VIII_Miss_Dad"))

# 確定新factor的排序
levels(dta$socII)
## [1] "I"             "II"            "III_0man"      "III_man"      
## [5] "IV"            "V"             "VI_Unemp_L"    "VII_emp_NC"   
## [9] "VIII_Miss_Dad"
# 以新水準進行繪圖
plot(x = dta$socII, y = dta$math, 
     ylab = "Mathematics test score",
     xlab = "Social Class",
     cex.axis = 0.7)

EX3-3

# 寫新的csv
write.csv(dta, "ex03c20180319.csv")