#下載資料: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)
#讀取資料
x <- read.csv("Transformed Data Set - Sheet1.csv", stringsAsFactors = TRUE)
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) <- c("color", "music", "beverage", "drink", "gender") 
 

#問題,男女對顏色的喜好的差異?

#次數分配表


t<- table(x$gender, x$color)
t
##    
##     Cool Neutral Warm
##   F   17       3   13
##   M   20       4    9
#百分比次數分配表
p.t <- prop.table(t)
p.t
##    
##           Cool    Neutral       Warm
##   F 0.25757576 0.04545455 0.19696970
##   M 0.30303030 0.06060606 0.13636364
p.t*100
##    
##          Cool   Neutral      Warm
##   F 25.757576  4.545455 19.696970
##   M 30.303030  6.060606 13.636364
p.t <- round(p.t*100, 2)     #先乘一百再四捨五入至小數第二位
  
  
  
  
  #畫分組長條圖
  barplot(p.t)

  barplot(p.t, beside = TRUE)

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

t<- table(x$gender, x$drink)
t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F          8              17     6     2
##   M          5              15     8     5
#百分比次數分配表
p.t <- prop.table(t)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi      Fanta      Other
##   F 0.12121212      0.25757576 0.09090909 0.03030303
##   M 0.07575758      0.22727273 0.12121212 0.07575758
p.t*100
##    
##     7UP/Sprite Coca Cola/Pepsi     Fanta     Other
##   F  12.121212       25.757576  9.090909  3.030303
##   M   7.575758       22.727273 12.121212  7.575758
p.t <- round(p.t*100, 2)     #先乘一百再四捨五入至小數第二位




#畫分組長條圖
barplot(p.t)

barplot(p.t, beside = TRUE)

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

#畫圓餅圖


#畫圓餅圖並加上資料標籤







colnames(x)<-c("color", "music", "beverage", "drink", "gender")     
x
##      color            music      beverage           drink gender
## 1     Cool             Rock         Vodka      7UP/Sprite      F
## 2  Neutral          Hip hop         Vodka Coca Cola/Pepsi      F
## 3     Warm             Rock          Wine Coca Cola/Pepsi      F
## 4     Warm Folk/Traditional       Whiskey           Fanta      F
## 5     Cool             Rock         Vodka Coca Cola/Pepsi      F
## 6     Warm       Jazz/Blues Doesn't drink           Fanta      F
## 7     Cool              Pop          Beer Coca Cola/Pepsi      F
## 8     Warm              Pop       Whiskey           Fanta      F
## 9     Warm             Rock         Other      7UP/Sprite      F
## 10 Neutral              Pop          Wine Coca Cola/Pepsi      F
## 11    Cool              Pop         Other      7UP/Sprite      F
## 12    Warm              Pop         Other      7UP/Sprite      F
## 13    Warm              Pop          Wine      7UP/Sprite      F
## 14    Warm       Electronic          Wine Coca Cola/Pepsi      F
## 15    Cool             Rock          Beer Coca Cola/Pepsi      F
## 16    Warm       Jazz/Blues          Wine Coca Cola/Pepsi      F
## 17    Cool              Pop          Wine      7UP/Sprite      F
## 18    Cool             Rock         Other Coca Cola/Pepsi      F
## 19    Cool             Rock         Other Coca Cola/Pepsi      F
## 20    Cool              Pop Doesn't drink      7UP/Sprite      F
## 21    Cool              Pop          Beer           Fanta      F
## 22    Warm       Jazz/Blues       Whiskey           Fanta      F
## 23    Cool             Rock         Vodka Coca Cola/Pepsi      F
## 24    Warm              Pop         Other Coca Cola/Pepsi      F
## 25    Cool Folk/Traditional       Whiskey      7UP/Sprite      F
## 26    Warm     R&B and soul       Whiskey Coca Cola/Pepsi      F
## 27    Cool              Pop          Beer           Other      F
## 28    Cool              Pop Doesn't drink           Other      F
## 29    Cool              Pop Doesn't drink Coca Cola/Pepsi      F
## 30    Cool       Electronic Doesn't drink           Fanta      F
## 31    Warm             Rock         Other Coca Cola/Pepsi      F
## 32 Neutral             Rock          Beer Coca Cola/Pepsi      F
## 33    Cool     R&B and soul          Beer Coca Cola/Pepsi      F
## 34    Warm     R&B and soul          Wine           Other      M
## 35 Neutral          Hip hop          Beer      7UP/Sprite      M
## 36    Warm       Electronic         Other Coca Cola/Pepsi      M
## 37 Neutral             Rock Doesn't drink Coca Cola/Pepsi      M
## 38    Cool              Pop         Other           Fanta      M
## 39    Cool              Pop       Whiskey           Fanta      M
## 40    Warm             Rock         Vodka      7UP/Sprite      M
## 41    Cool             Rock         Vodka Coca Cola/Pepsi      M
## 42 Neutral              Pop Doesn't drink      7UP/Sprite      M
## 43    Warm     R&B and soul Doesn't drink Coca Cola/Pepsi      M
## 44    Cool             Rock          Wine      7UP/Sprite      M
## 45    Cool Folk/Traditional          Beer           Other      M
## 46    Cool          Hip hop          Beer Coca Cola/Pepsi      M
## 47    Cool          Hip hop          Wine Coca Cola/Pepsi      M
## 48    Cool     R&B and soul       Whiskey      7UP/Sprite      M
## 49    Cool             Rock Doesn't drink           Other      M
## 50    Warm          Hip hop          Beer Coca Cola/Pepsi      M
## 51    Cool     R&B and soul Doesn't drink Coca Cola/Pepsi      M
## 52    Cool             Rock Doesn't drink Coca Cola/Pepsi      M
## 53    Cool          Hip hop Doesn't drink           Other      M
## 54    Warm             Rock          Beer           Fanta      M
## 55    Cool       Electronic Doesn't drink           Fanta      M
## 56    Cool       Electronic         Other           Fanta      M
## 57    Warm Folk/Traditional         Other           Fanta      M
## 58    Warm       Electronic         Vodka           Fanta      M
## 59    Warm       Jazz/Blues         Vodka Coca Cola/Pepsi      M
## 60    Cool              Pop       Whiskey           Other      M
## 61    Cool       Electronic       Whiskey Coca Cola/Pepsi      M
## 62    Cool             Rock         Vodka Coca Cola/Pepsi      M
## 63    Cool          Hip hop          Beer Coca Cola/Pepsi      M
## 64 Neutral          Hip hop Doesn't drink           Fanta      M
## 65    Cool             Rock          Wine Coca Cola/Pepsi      M
## 66    Cool       Electronic          Beer Coca Cola/Pepsi      M
#問題,男女對顏色的喜好的差異?

#次數分配表
#table=算次數的函數 table(列,欄)
t <- table(x$gender, x$drink)
t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F          8              17     6     2
##   M          5              15     8     5
#百分比次數分配表
p.t <- prop.table(t)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi      Fanta      Other
##   F 0.12121212      0.25757576 0.09090909 0.03030303
##   M 0.07575758      0.22727273 0.12121212 0.07575758
p.t*100
##    
##     7UP/Sprite Coca Cola/Pepsi     Fanta     Other
##   F  12.121212       25.757576  9.090909  3.030303
##   M   7.575758       22.727273 12.121212  7.575758
#round=取小數(,2)四捨五入取道第二位
p.t <-round(p.t*100,2)
p.t
##    
##     7UP/Sprite Coca Cola/Pepsi Fanta Other
##   F      12.12           25.76  9.09  3.03
##   M       7.58           22.73 12.12  7.58
#畫分組長條圖
barplot(p.t)

barplot(p.t,beside = TRUE)

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