##R語言資料分析期中考:
##輸入學號: 001 和名字:Shaq
##壹、自建資料與圖表(請見社團範例)
#(1-1)建立一個data frame,包括brand品牌與market_share市站率
#(1-2)畫長條圖
brand<-c("Samsung","Apple","Oppo")
market_share<-c(31.7,22.8,9.3)
TU<-data.frame(brand,market_share )
TU
## brand market_share
## 1 Samsung 31.7
## 2 Apple 22.8
## 3 Oppo 9.3
barplot(sort( market_share,decreasing = TRUE),
names.arg =c("Samsung","Apple","Oppo"),
main = "TU",
sub="by tu chin hau",
xlab = "brand",
ylab = " market_share",
col =c("purple","blue","red"))

#貳、比較gender 性別與lunch 營養午餐類型
#讀入外部資料
sp <- read.csv(file = "StudentsPerformance.csv", stringsAsFactors = TRUE)
summary(sp)
## gender race.ethnicity parental.level.of.education lunch
## female:518 group A: 89 associate's degree:222 free/reduced:355
## male :482 group B:190 bachelor's degree :118 standard :645
## group C:319 high school :196
## group D:262 master's degree : 59
## group E:140 some college :226
## some high school :179
## test.preparation.course math.score reading.score writing.score
## completed:358 Min. : 0.00 Min. : 17.00 Min. : 10.00
## none :642 1st Qu.: 57.00 1st Qu.: 59.00 1st Qu.: 57.75
## Median : 66.00 Median : 70.00 Median : 69.00
## Mean : 66.09 Mean : 69.17 Mean : 68.05
## 3rd Qu.: 77.00 3rd Qu.: 79.00 3rd Qu.: 79.00
## Max. :100.00 Max. :100.00 Max. :100.00
#欄位名稱------------------------------------------
# gender 性別
# race.ethnicity 種族分群
# parental.level.of.education 父母教育程度
# lunch 營養午餐類型(free/reduced免費或減免餐費,standard為一般類別)
# test.preparation.course
# math.score 數學成績
# reading.score 閱讀成績
# writing.score 寫作成績
#(2-1)計算不同gender 性別與lunch 營養午餐人數的比例(交叉分析表)
t <- table(sp$lunch,sp$gender)
t
##
## female male
## free/reduced 189 166
## standard 329 316
p.t <- prop.table(t)
p.t
##
## female male
## free/reduced 0.189 0.166
## standard 0.329 0.316
p.t <- p.t*100
p.t
##
## female male
## free/reduced 18.9 16.6
## standard 32.9 31.6
#(2-2)呈現gender 性別與lunch 營養午餐人數的圖表
rownames(p.t)
## [1] "free/reduced" "standard"
label <- rownames(p.t)
label
## [1] "free/reduced" "standard"
barplot(p.t,
beside =TRUE ,
legend.text =label,
col =c(3,5) )

#參、math.score 數學成績的直方圖與盒狀圖
x<-sp
boxplot(x$math.score,col="blue")

#肆、math.score 數學成績的最大值、最小值、平均數、中位數、標準差
x$math.score
## [1] 72 69 90 47 76 71 88 40 64 38 58 40 65 78 50 69 88 18
## [19] 46 54 66 65 44 69 74 73 69 67 70 62 69 63 56 40 97 81
## [37] 74 50 75 57 55 58 53 59 50 65 55 66 57 82 53 77 53 88
## [55] 71 33 82 52 58 0 79 39 62 69 59 67 45 60 61 39 58 63
## [73] 41 61 49 44 30 80 61 62 47 49 50 72 42 73 76 71 58 73
## [91] 65 27 71 43 79 78 65 63 58 65 79 68 85 60 98 58 87 66
## [109] 52 70 77 62 54 51 99 84 75 78 51 55 79 91 88 63 83 87
## [127] 72 65 82 51 89 53 87 75 74 58 51 70 59 71 76 59 42 57
## [145] 88 22 88 73 68 100 62 77 59 54 62 70 66 60 61 66 82 75
## [163] 49 52 81 96 53 58 68 67 72 94 79 63 43 81 46 71 52 97
## [181] 62 46 50 65 45 65 80 62 48 77 66 76 62 77 69 61 59 55
## [199] 45 78 67 65 69 57 59 74 82 81 74 58 80 35 42 60 87 84
## [217] 83 34 66 61 56 87 55 86 52 45 72 57 68 88 76 46 67 92
## [235] 83 80 63 64 54 84 73 80 56 59 75 85 89 58 65 68 47 71
## [253] 60 80 54 62 64 78 70 65 64 79 44 99 76 59 63 69 88 71
## [271] 69 58 47 65 88 83 85 59 65 73 53 45 73 70 37 81 97 67
## [289] 88 77 76 86 63 65 78 67 46 71 40 90 81 56 67 80 74 69
## [307] 99 51 53 49 73 66 67 68 59 71 77 83 63 56 67 75 71 43
## [325] 41 82 61 28 82 41 71 47 62 90 83 61 76 49 24 35 58 61
## [343] 69 67 79 72 62 77 75 87 52 66 63 46 59 61 63 42 59 80
## [361] 58 85 52 27 59 49 69 61 44 73 84 45 74 82 59 46 80 85
## [379] 71 66 80 87 79 38 38 67 64 57 62 73 73 77 76 57 65 48
## [397] 50 85 74 60 59 53 49 88 54 63 65 82 52 87 70 84 71 63
## [415] 51 84 71 74 68 57 82 57 47 59 41 62 86 69 65 68 64 61
## [433] 61 47 73 50 75 75 70 89 67 78 59 73 79 67 69 86 47 81
## [451] 64 100 65 65 53 37 79 53 100 72 53 54 71 77 75 84 26 72
## [469] 77 91 83 63 68 59 90 71 76 80 55 76 73 52 68 59 49 70
## [487] 61 60 64 79 65 64 83 81 54 68 54 59 66 76 74 94 63 95
## [505] 40 82 68 55 79 86 76 64 62 54 77 76 74 66 66 67 71 91
## [523] 69 54 53 68 56 36 29 62 68 47 62 79 73 66 51 51 85 97
## [541] 75 79 81 82 64 78 92 72 62 79 79 87 40 77 53 32 55 61
## [559] 53 73 74 63 96 63 48 48 92 61 63 68 71 91 53 50 74 40
## [577] 61 81 48 53 81 77 63 73 69 65 55 44 54 48 58 71 68 74
## [595] 92 56 30 53 69 65 54 29 76 60 84 75 85 40 61 58 69 58
## [613] 94 65 82 60 37 88 95 65 35 62 58 100 61 100 69 61 49 44
## [631] 67 79 66 75 84 71 67 80 86 76 41 74 72 74 70 65 59 64
## [649] 50 69 51 68 85 65 73 62 77 69 43 90 74 73 55 65 80 50
## [667] 63 77 73 81 66 52 69 65 69 50 73 70 81 63 67 60 62 29
## [685] 62 94 85 77 53 93 49 73 66 77 49 79 75 59 57 66 79 57
## [703] 87 63 59 62 46 66 89 42 93 80 98 81 60 76 73 96 76 91
## [721] 62 55 74 50 47 81 65 68 73 53 68 55 87 55 53 67 92 53
## [739] 81 61 80 37 81 59 55 72 69 69 50 87 71 68 79 77 58 84
## [757] 55 70 52 69 53 48 78 62 60 74 58 76 68 58 52 75 52 62
## [775] 66 49 66 35 72 94 46 77 76 52 91 32 72 19 68 52 48 60
## [793] 66 89 42 57 70 70 69 52 67 76 87 82 73 75 64 41 90 59
## [811] 51 45 54 87 72 94 45 61 60 77 85 78 49 71 48 62 56 65
## [829] 69 68 61 74 64 77 58 60 73 75 58 66 39 64 23 74 40 90
## [847] 91 64 59 80 71 61 87 82 62 97 75 65 52 87 53 81 39 71
## [865] 97 82 59 61 78 49 59 70 82 90 43 80 81 57 59 64 63 71
## [883] 64 55 51 62 93 54 69 44 86 85 50 88 59 32 36 63 67 65
## [901] 85 73 34 93 67 88 57 79 67 70 50 69 52 47 46 68 100 44
## [919] 57 91 69 35 72 54 74 74 64 65 46 48 67 62 61 70 98 70
## [937] 67 57 85 77 72 78 81 61 58 54 82 49 49 57 94 75 74 58
## [955] 62 72 84 92 45 75 56 48 100 65 72 62 66 63 68 75 89 78
## [973] 53 49 54 64 60 62 55 91 8 81 79 78 74 57 40 81 44 67
## [991] 86 65 55 62 63 88 62 59 68 77
max(x$math.score)
## [1] 100
min(x$math.score)
## [1] 0
mean(x$math.score)
## [1] 66.089
median(x$math.score)
## [1] 66
sd(x$math.score)
## [1] 15.16308
#五、呈現以下兩組關係的散佈圖
#(1)math.score 數學成績與與writing.score 寫作成績
plot(sp$math.score,sp$writing.score)

#(2)reading.score 閱讀成績與writing.score 寫作成績
plot(sp$reading.score,sp$writing.score)

#六、計算以下兩組關係的相關係數
#(1)reading.score 閱讀成績與math.score 數學成績
cor(sp$reading.score,sp$math.score)
## [1] 0.8175797
#(2)writing.score 寫作成績與math.score 數學成績
cor(sp$writing.score,sp$math.score)
## [1] 0.802642
#七、計算不同gender 性別的math.score 數學成績並畫長條圖
t <- table(sp$gender, sp$math.score)
t
##
## 0 8 18 19 22 23 24 26 27 28 29 30 32 33 34 35 36 37 38 39 40 41 42
## female 1 1 1 1 1 1 1 1 1 0 3 0 3 1 2 4 1 3 3 2 5 4 5
## male 0 0 0 0 0 0 0 0 1 1 0 2 0 0 0 1 1 1 0 2 5 2 1
##
## 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
## female 4 6 5 5 6 7 8 11 6 13 11 11 8 7 10 13 21 8 11 16 13 12 28
## male 1 3 4 6 5 4 9 4 5 5 13 7 10 2 8 12 11 8 16 19 13 8 8
##
## 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
## female 12 16 11 16 8 13 7 13 15 10 8 16 6 8 6 8 9 4 2 8 2 5 6
## male 12 10 15 16 10 13 11 14 10 11 13 8 8 14 11 14 9 4 9 6 6 11 9
##
## 89 90 91 92 93 94 95 96 97 98 99 100
## female 1 2 2 4 2 2 1 2 2 1 2 3
## male 5 6 7 2 2 5 1 1 4 2 1 4
x<-tapply(sp$math.score,sp$gender,mean)
x
## female male
## 63.63320 68.72822
barplot(x,main = "gender", sub = "by eyon",xlab="gender",ylab="grade",
beside =TRUE ,
col =c(5:4))
