#上課程式碼:

#資料介紹:https://www.kaggle.com/hb20007/gender-classification?select=Transformed+Data+Set+-+Sheet1.csv
#檔名為"Transformed Data Set - Sheet1.csv"
#從電腦端上傳(upload)到rstudio cloud

#install.packages("readr")#第一次使用需安裝
library(readr)#用library呼叫套件
#讀取資料,命名為"x"
x <- read.csv("Transformed Data Set - Sheet1.csv", stringsAsFactors = TRUE)
x
##    Favorite.Color Favorite.Music.Genre Favorite.Beverage Favorite.Soft.Drink
## 1            Cool                 Rock             Vodka          7UP/Sprite
## 2         Neutral              Hip hop             Vodka     Coca Cola/Pepsi
## 3            Warm                 Rock              Wine     Coca Cola/Pepsi
## 4            Warm     Folk/Traditional           Whiskey               Fanta
## 5            Cool                 Rock             Vodka     Coca Cola/Pepsi
## 6            Warm           Jazz/Blues     Doesn't drink               Fanta
## 7            Cool                  Pop              Beer     Coca Cola/Pepsi
## 8            Warm                  Pop           Whiskey               Fanta
## 9            Warm                 Rock             Other          7UP/Sprite
## 10        Neutral                  Pop              Wine     Coca Cola/Pepsi
## 11           Cool                  Pop             Other          7UP/Sprite
## 12           Warm                  Pop             Other          7UP/Sprite
## 13           Warm                  Pop              Wine          7UP/Sprite
## 14           Warm           Electronic              Wine     Coca Cola/Pepsi
## 15           Cool                 Rock              Beer     Coca Cola/Pepsi
## 16           Warm           Jazz/Blues              Wine     Coca Cola/Pepsi
## 17           Cool                  Pop              Wine          7UP/Sprite
## 18           Cool                 Rock             Other     Coca Cola/Pepsi
## 19           Cool                 Rock             Other     Coca Cola/Pepsi
## 20           Cool                  Pop     Doesn't drink          7UP/Sprite
## 21           Cool                  Pop              Beer               Fanta
## 22           Warm           Jazz/Blues           Whiskey               Fanta
## 23           Cool                 Rock             Vodka     Coca Cola/Pepsi
## 24           Warm                  Pop             Other     Coca Cola/Pepsi
## 25           Cool     Folk/Traditional           Whiskey          7UP/Sprite
## 26           Warm         R&B and soul           Whiskey     Coca Cola/Pepsi
## 27           Cool                  Pop              Beer               Other
## 28           Cool                  Pop     Doesn't drink               Other
## 29           Cool                  Pop     Doesn't drink     Coca Cola/Pepsi
## 30           Cool           Electronic     Doesn't drink               Fanta
## 31           Warm                 Rock             Other     Coca Cola/Pepsi
## 32        Neutral                 Rock              Beer     Coca Cola/Pepsi
## 33           Cool         R&B and soul              Beer     Coca Cola/Pepsi
## 34           Warm         R&B and soul              Wine               Other
## 35        Neutral              Hip hop              Beer          7UP/Sprite
## 36           Warm           Electronic             Other     Coca Cola/Pepsi
## 37        Neutral                 Rock     Doesn't drink     Coca Cola/Pepsi
## 38           Cool                  Pop             Other               Fanta
## 39           Cool                  Pop           Whiskey               Fanta
## 40           Warm                 Rock             Vodka          7UP/Sprite
## 41           Cool                 Rock             Vodka     Coca Cola/Pepsi
## 42        Neutral                  Pop     Doesn't drink          7UP/Sprite
## 43           Warm         R&B and soul     Doesn't drink     Coca Cola/Pepsi
## 44           Cool                 Rock              Wine          7UP/Sprite
## 45           Cool     Folk/Traditional              Beer               Other
## 46           Cool              Hip hop              Beer     Coca Cola/Pepsi
## 47           Cool              Hip hop              Wine     Coca Cola/Pepsi
## 48           Cool         R&B and soul           Whiskey          7UP/Sprite
## 49           Cool                 Rock     Doesn't drink               Other
## 50           Warm              Hip hop              Beer     Coca Cola/Pepsi
## 51           Cool         R&B and soul     Doesn't drink     Coca Cola/Pepsi
## 52           Cool                 Rock     Doesn't drink     Coca Cola/Pepsi
## 53           Cool              Hip hop     Doesn't drink               Other
## 54           Warm                 Rock              Beer               Fanta
## 55           Cool           Electronic     Doesn't drink               Fanta
## 56           Cool           Electronic             Other               Fanta
## 57           Warm     Folk/Traditional             Other               Fanta
## 58           Warm           Electronic             Vodka               Fanta
## 59           Warm           Jazz/Blues             Vodka     Coca Cola/Pepsi
## 60           Cool                  Pop           Whiskey               Other
## 61           Cool           Electronic           Whiskey     Coca Cola/Pepsi
## 62           Cool                 Rock             Vodka     Coca Cola/Pepsi
## 63           Cool              Hip hop              Beer     Coca Cola/Pepsi
## 64        Neutral              Hip hop     Doesn't drink               Fanta
## 65           Cool                 Rock              Wine     Coca Cola/Pepsi
## 66           Cool           Electronic              Beer     Coca Cola/Pepsi
##    Gender
## 1       F
## 2       F
## 3       F
## 4       F
## 5       F
## 6       F
## 7       F
## 8       F
## 9       F
## 10      F
## 11      F
## 12      F
## 13      F
## 14      F
## 15      F
## 16      F
## 17      F
## 18      F
## 19      F
## 20      F
## 21      F
## 22      F
## 23      F
## 24      F
## 25      F
## 26      F
## 27      F
## 28      F
## 29      F
## 30      F
## 31      F
## 32      F
## 33      F
## 34      M
## 35      M
## 36      M
## 37      M
## 38      M
## 39      M
## 40      M
## 41      M
## 42      M
## 43      M
## 44      M
## 45      M
## 46      M
## 47      M
## 48      M
## 49      M
## 50      M
## 51      M
## 52      M
## 53      M
## 54      M
## 55      M
## 56      M
## 57      M
## 58      M
## 59      M
## 60      M
## 61      M
## 62      M
## 63      M
## 64      M
## 65      M
## 66      M
#查看資料結構
summary(x)
##  Favorite.Color       Favorite.Music.Genre     Favorite.Beverage
##  Cool   :37     Electronic      : 8        Beer         :13     
##  Neutral: 7     Folk/Traditional: 4        Doesn't drink:14     
##  Warm   :22     Hip hop         : 8        Other        :11     
##                 Jazz/Blues      : 4        Vodka        : 9     
##                 Pop             :17        Whiskey      : 9     
##                 R&B and soul    : 6        Wine         :10     
##                 Rock            :19                             
##       Favorite.Soft.Drink Gender
##  7UP/Sprite     :13       F:33  
##  Coca Cola/Pepsi:32       M:33  
##  Fanta          :14             
##  Other          : 7             
##                                 
##                                 
## 
#重新命名欄位名稱
colnames(x) #檢視欄位名稱
## [1] "Favorite.Color"       "Favorite.Music.Genre" "Favorite.Beverage"   
## [4] "Favorite.Soft.Drink"  "Gender"
colnames(x) <- c("color", "music", "beverage", "drink", "gender")     

###類別資料分析###

#問題:男女生各有多少人?
table(x$gender)#算次數
## 
##  F  M 
## 33 33
prop.table(table(x$gender))#算百分比
## 
##   F   M 
## 0.5 0.5
#問題:最喜歡的color、music、beverage、 drink、gender?
table(x$color)
## 
##    Cool Neutral    Warm 
##      37       7      22
prop.table(table(x$color))
## 
##      Cool   Neutral      Warm 
## 0.5606061 0.1060606 0.3333333
#問題:男女對顏色的喜好的差異?

#次數分配表
t<-table(x$gender, x$color)
t
##    
##     Cool Neutral Warm
##   F   17       3   13
##   M   20       4    9
#百分比次數分配表
p.t <- prop.table(t,2)
p.t
##    
##          Cool   Neutral      Warm
##   F 0.4594595 0.4285714 0.5909091
##   M 0.5405405 0.5714286 0.4090909
#將次數變成百分比(乘以100)
p.t <-p.t*100 
p.t
##    
##         Cool  Neutral     Warm
##   F 45.94595 42.85714 59.09091
##   M 54.05405 57.14286 40.90909
#四捨五入至小數2位
p.t <- round(p.t,2)
p.t
##    
##      Cool Neutral  Warm
##   F 45.95   42.86 59.09
##   M 54.05   57.14 40.91
#畫分組長條圖
barplot(p.t)

barplot(p.t, beside = T   )

#不同性別喜歡無酒精飲料差別
table(x$drink)
## 
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##              13              32              14               7
prop.table(table(x$drink))
## 
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##       0.1969697       0.4848485       0.2121212       0.1060606
e<-table(x$gender, x$drink )
e
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F          8              17     6     2
##   M          5              15     8     5
#百分比次數分配表
p.e <- prop.table(e,2)
p.e
##    
##     7UP/Sprite Coca Cola/Pepsi     Fanta     Other
##   F  0.6153846       0.5312500 0.4285714 0.2857143
##   M  0.3846154       0.4687500 0.5714286 0.7142857
#將次數變成百分比(乘以100)
p.e <-p.e*100 
p.e
##    
##     7UP/Sprite Coca Cola/Pepsi    Fanta    Other
##   F   61.53846        53.12500 42.85714 28.57143
##   M   38.46154        46.87500 57.14286 71.42857
#四捨五入至小數2位
p.e<- round(p.e,2)
p.e
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      61.54           53.12 42.86 28.57
##   M      38.46           46.88 57.14 71.43
barplot(p.e)

barplot(p.e, beside = T   )

#加上圖例與上色
rownames(p.t)
## [1] "F" "M"
label <- rownames(p.t)
label
## [1] "F" "M"
barplot(p.t, 
        beside = T, 
        legend.text =label, 
        col =c(26,1))

#飲料
rownames(p.e)
## [1] "F" "M"
label <- rownames(p.e)
label
## [1] "F" "M"
barplot(p.e, 
        beside = T, 
        legend.text =label, 
        col =c(33,19))

#畫圓餅圖
p.t
##    
##      Cool Neutral  Warm
##   F 45.95   42.86 59.09
##   M 54.05   57.14 40.91
f <- p.t[1,] # 女性資料
m <- p.t[2,] # 男性資料
f
##    Cool Neutral    Warm 
##   45.95   42.86   59.09
m
##    Cool Neutral    Warm 
##   54.05   57.14   40.91
# par()是圖形控制函數,mfrow = c(1,2)表示建立一個1x2的空間,用來呈現後續的圖1列2欄
par(mfrow = c(1,2)   )
pie(f, main ="女森"   )
pie(m, main = "男森")

dev.off()  #離開par()
## null device 
##           1
#畫圓餅圖並加上資料標籤
pie_category <- colnames(p.t)
pie_category 
## [1] "Cool"    "Neutral" "Warm"
f_label <- paste(pie_category, f,"%", sep = "")
f_label
## [1] "Cool45.95%"    "Neutral42.86%" "Warm59.09%"
m_label <- paste(pie_category, m,"%", sep = "")
m_label
## [1] "Cool54.05%"    "Neutral57.14%" "Warm40.91%"
par()# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
## $xlog
## [1] FALSE
## 
## $ylog
## [1] FALSE
## 
## $adj
## [1] 0.5
## 
## $ann
## [1] TRUE
## 
## $ask
## [1] FALSE
## 
## $bg
## [1] "transparent"
## 
## $bty
## [1] "o"
## 
## $cex
## [1] 1
## 
## $cex.axis
## [1] 1
## 
## $cex.lab
## [1] 1
## 
## $cex.main
## [1] 1.2
## 
## $cex.sub
## [1] 1
## 
## $cin
## [1] 0.15 0.20
## 
## $col
## [1] "black"
## 
## $col.axis
## [1] "black"
## 
## $col.lab
## [1] "black"
## 
## $col.main
## [1] "black"
## 
## $col.sub
## [1] "black"
## 
## $cra
## [1] 10.8 14.4
## 
## $crt
## [1] 0
## 
## $csi
## [1] 0.2
## 
## $cxy
## [1] 0.02604167 0.03875969
## 
## $din
## [1] 7 7
## 
## $err
## [1] 0
## 
## $family
## [1] ""
## 
## $fg
## [1] "black"
## 
## $fig
## [1] 0 1 0 1
## 
## $fin
## [1] 7 7
## 
## $font
## [1] 1
## 
## $font.axis
## [1] 1
## 
## $font.lab
## [1] 1
## 
## $font.main
## [1] 2
## 
## $font.sub
## [1] 1
## 
## $lab
## [1] 5 5 7
## 
## $las
## [1] 0
## 
## $lend
## [1] "round"
## 
## $lheight
## [1] 1
## 
## $ljoin
## [1] "round"
## 
## $lmitre
## [1] 10
## 
## $lty
## [1] "solid"
## 
## $lwd
## [1] 1
## 
## $mai
## [1] 1.02 0.82 0.82 0.42
## 
## $mar
## [1] 5.1 4.1 4.1 2.1
## 
## $mex
## [1] 1
## 
## $mfcol
## [1] 1 1
## 
## $mfg
## [1] 1 1 1 1
## 
## $mfrow
## [1] 1 1
## 
## $mgp
## [1] 3 1 0
## 
## $mkh
## [1] 0.001
## 
## $new
## [1] FALSE
## 
## $oma
## [1] 0 0 0 0
## 
## $omd
## [1] 0 1 0 1
## 
## $omi
## [1] 0 0 0 0
## 
## $page
## [1] TRUE
## 
## $pch
## [1] 1
## 
## $pin
## [1] 5.76 5.16
## 
## $plt
## [1] 0.1171429 0.9400000 0.1457143 0.8828571
## 
## $ps
## [1] 12
## 
## $pty
## [1] "m"
## 
## $smo
## [1] 1
## 
## $srt
## [1] 0
## 
## $tck
## [1] NA
## 
## $tcl
## [1] -0.5
## 
## $usr
## [1] 0 1 0 1
## 
## $xaxp
## [1] 0 1 5
## 
## $xaxs
## [1] "r"
## 
## $xaxt
## [1] "s"
## 
## $xpd
## [1] FALSE
## 
## $yaxp
## [1] 0 1 5
## 
## $yaxs
## [1] "r"
## 
## $yaxt
## [1] "s"
## 
## $ylbias
## [1] 0.2
pie(f, labels =  f_label, main = "Female")
pie(m, labels =  m_label, main = "Male" )
dev.off()  #離開par()
## null device 
##           1
#畫圓餅圖
p.e
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      61.54           53.12 42.86 28.57
##   M      38.46           46.88 57.14 71.43
f <- p.e[1,] # 女性資料
m <- p.e[2,] # 男性資料
f
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##           61.54           53.12           42.86           28.57
m
##      7UP/Sprite Coca Cola/Pepsi           Fanta           Other 
##           38.46           46.88           57.14           71.43
# par()是圖形控制函數,mfrow = c(1,2)表示建立一個1x2的空間,用來呈現後續的圖1列2欄
par(mfrow = c(1,2)   )
pie(f, main ="女森"   )
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <e5>
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <a5>
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <b3>
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <e6>
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <a3>
## Warning in title(main = main, ...): conversion failure on '女森' in
## 'mbcsToSbcs': dot substituted for <ae>
pie(m, main = "男森")
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <e7>
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <94>
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <b7>
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <e6>
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <a3>
## Warning in title(main = main, ...): conversion failure on '男森' in
## 'mbcsToSbcs': dot substituted for <ae>
dev.off()  #離開par()
## null device 
##           1
#畫圓餅圖並加上資料標籤
pie_category <- colnames(p.e)
pie_category 
## [1] "7UP/Sprite"      "Coca Cola/Pepsi" "Fanta"           "Other"
f_label <- paste(pie_category, f,"%", sep = "")
f_label
## [1] "7UP/Sprite61.54%"      "Coca Cola/Pepsi53.12%" "Fanta42.86%"          
## [4] "Other28.57%"
m_label <- paste(pie_category, m,"%", sep = "")
m_label
## [1] "7UP/Sprite38.46%"      "Coca Cola/Pepsi46.88%" "Fanta57.14%"          
## [4] "Other71.43%"
par()# c(1,2),表示建立一個1x2的空間,用來呈現後續的圖
## $xlog
## [1] FALSE
## 
## $ylog
## [1] FALSE
## 
## $adj
## [1] 0.5
## 
## $ann
## [1] TRUE
## 
## $ask
## [1] FALSE
## 
## $bg
## [1] "transparent"
## 
## $bty
## [1] "o"
## 
## $cex
## [1] 1
## 
## $cex.axis
## [1] 1
## 
## $cex.lab
## [1] 1
## 
## $cex.main
## [1] 1.2
## 
## $cex.sub
## [1] 1
## 
## $cin
## [1] 0.15 0.20
## 
## $col
## [1] "black"
## 
## $col.axis
## [1] "black"
## 
## $col.lab
## [1] "black"
## 
## $col.main
## [1] "black"
## 
## $col.sub
## [1] "black"
## 
## $cra
## [1] 10.8 14.4
## 
## $crt
## [1] 0
## 
## $csi
## [1] 0.2
## 
## $cxy
## [1] 0.02604167 0.03875969
## 
## $din
## [1] 7 7
## 
## $err
## [1] 0
## 
## $family
## [1] ""
## 
## $fg
## [1] "black"
## 
## $fig
## [1] 0 1 0 1
## 
## $fin
## [1] 7 7
## 
## $font
## [1] 1
## 
## $font.axis
## [1] 1
## 
## $font.lab
## [1] 1
## 
## $font.main
## [1] 2
## 
## $font.sub
## [1] 1
## 
## $lab
## [1] 5 5 7
## 
## $las
## [1] 0
## 
## $lend
## [1] "round"
## 
## $lheight
## [1] 1
## 
## $ljoin
## [1] "round"
## 
## $lmitre
## [1] 10
## 
## $lty
## [1] "solid"
## 
## $lwd
## [1] 1
## 
## $mai
## [1] 1.02 0.82 0.82 0.42
## 
## $mar
## [1] 5.1 4.1 4.1 2.1
## 
## $mex
## [1] 1
## 
## $mfcol
## [1] 1 1
## 
## $mfg
## [1] 1 1 1 1
## 
## $mfrow
## [1] 1 1
## 
## $mgp
## [1] 3 1 0
## 
## $mkh
## [1] 0.001
## 
## $new
## [1] FALSE
## 
## $oma
## [1] 0 0 0 0
## 
## $omd
## [1] 0 1 0 1
## 
## $omi
## [1] 0 0 0 0
## 
## $page
## [1] TRUE
## 
## $pch
## [1] 1
## 
## $pin
## [1] 5.76 5.16
## 
## $plt
## [1] 0.1171429 0.9400000 0.1457143 0.8828571
## 
## $ps
## [1] 12
## 
## $pty
## [1] "m"
## 
## $smo
## [1] 1
## 
## $srt
## [1] 0
## 
## $tck
## [1] NA
## 
## $tcl
## [1] -0.5
## 
## $usr
## [1] 0 1 0 1
## 
## $xaxp
## [1] 0 1 5
## 
## $xaxs
## [1] "r"
## 
## $xaxt
## [1] "s"
## 
## $xpd
## [1] FALSE
## 
## $yaxp
## [1] 0 1 5
## 
## $yaxs
## [1] "r"
## 
## $yaxt
## [1] "s"
## 
## $ylbias
## [1] 0.2
pie(f, labels =  f_label, main = "Female")
pie(m, labels =  m_label, main = "Male" )
dev.off()  #離開par()
## null device 
##           1