#0501課堂作業

#請讀取superheroes.RData
load("superheroes.RData")
#一、請將Alignment的資料型態轉換為facor, levels 依序為"good", "neutral", "bad"
X <- superheroes
str(X$Alignment)
##  chr [1:428] "good" "good" "good" "good" "bad" "bad" "good" "good" "good" ...
X$Alignment<- factor(X$Alignment, 
                     levels= c("good","neutral","bad"),
                     labels = c("好的", "中性的", "壞的"))
#二、請計算Alignment裡的good", "neutral"和"bad"的佔比為何?
str(X$Alignment)
##  Factor w/ 3 levels "好的","中性的",..: 1 1 1 1 3 3 1 1 1 3 ...
summary(X$Alignment)
##   好的 中性的   壞的 
##    296     11    121
#三、請畫出Alignment的長條圖和圓餅圖。
#長條圖
#圓餅圖
a <- table(X$Alignment)
a
## 
##   好的 中性的   壞的 
##    296     11    121
p.g <- proportions(a)
p.g <- p.g*100
p.g <- round(p.g, 2)#四捨五入
p.g
## 
##   好的 中性的   壞的 
##  69.16   2.57  28.27
pie_category <-names (p.g)
pie_category
## [1] "好的"   "中性的" "壞的"
pie_label <- paste(pie_category,p.g ,"%", sep = "")
pie_label
## [1] "好的69.16%"  "中性的2.57%" "壞的28.27%"
pie(p.g, 
    labels =pie_label, 
    col = c(7:9),
    main = "Alignment")

barplot(p.g,
        col = c(7:9),
        main= "Alignment",
        ylab= "百分比")

#四、請畫出speed的直方圖與盒狀圖。
boxplot(X$Speed, main = "速度盒狀圖")

hist(X$Speed, main = "速度直方圖")

#五、請計算Strength與Durability的相關係數,並畫出散佈圖。
cor(X$Strength, X$Durability)
## [1] 0.6843862
plot(X$Strength, X$Durability)

#六、請問好人們和壞人們誰比較強?