DAY 7
1
AI & Data

## [Data Science系列]運用R語言Factor因子來分析資料-哥布林村的人口激增？

"恩君，恩君～"村長氣喘呼呼的跑了過來
"怎麼了村長？"
"最近村子來了很多不同種族的魔物，聽說你幫你叔叔帳目打理得很好阿，特地跑來早你做一下整理"

`````` PUP
[1] HO GO HO H  O  O  GO GO O  E  H  E  GO E  E  HO H  HO O  HO O  GO H  HO GO H  H  GO E  HO
[31] HO H  GO O  HO H  GO E  HO HO H  E  GO GO HO O  GO GO E  GO
``````

``````is.vector(PUP)
[1] TRUE

is.factor(PUP)
[1] FALSE
``````

``````PUP_factor <- factor(PUP)
PUP_factor

HO GO HO H O O GO GO O E H E GO E E HO H HO O HO O GO H HO GO H H GO E HO HO H GO O HO H GO E HO HO H E GO GO HO O GO GO E GO
Levels:
'E' 'GO' 'H' 'HO' 'O'

levels(PUP_factor) <- c("Elf", "Goblin", "Harpy", "Hoblin", "Orc")
``````

``````PUP_factor <- factor(PUP, levels = c("O", "E", "GO", "H", "HO"),
labels = c("Orc", "Elf", "Goblin", "Harpy", "Hoblin"))

PUP_factor

Hoblin Goblin Hoblin Harpy Orc Orc Goblin Goblin Orc Elf Harpy Elf Goblin Elf Elf Hoblin Harpy Hoblin Orc Hoblin Orc Goblin Harpy Hoblin Goblin Harpy Harpy Goblin Elf Hoblin Hoblin Harpy Goblin Orc Hoblin Harpy Goblin Elf Hoblin Hoblin Harpy Elf Goblin Goblin Hoblin Orc Goblin Goblin Elf Goblin

Levels:
'Orc' 'Elf' 'Goblin' 'Harpy' 'Hoblin'
``````

"很好阿，不過每個種族特性不同阿，比如精靈族最有錢，哥布林族最窮，我們怎麼樣才可以知道或如何來做比較呢"

``````PUP_wealth_factor <- factor(PUP_factor, ordered = TRUE, levels = c("Goblin", "Hoblin", "Orc", "Harpy", "Elf"))

PUP_wealth_factor
Hoblin Goblin Hoblin Harpy Orc Orc Goblin Goblin Orc Elf Harpy Elf Goblin Elf Elf Hoblin Harpy Hoblin Orc Hoblin Orc Goblin Harpy Hoblin Goblin Harpy Harpy Goblin Elf Hoblin Hoblin Harpy Goblin Orc Hoblin Harpy Goblin Elf Hoblin Hoblin Harpy Elf Goblin Goblin Hoblin Orc Goblin Goblin Elf Goblin

Levels:
'Goblin' 'Hoblin' 'Orc' 'Harpy' 'Elf'
``````

``````PUP_wealth_factor[49] > PUP_wealth_factor[1]
[1] TRUE
``````

``````summary(PUP_wealth_factor)

Goblin    Hoblin    Orc    Harpy    Elf
14       12      7        9      8

str(PUP_wealth_factor)
Ord.factor w/ 5 levels "Goblin"<"Hoblin"<..: 2 1 2 4 3 3 1 1 3 5 ...
``````

str就是structure，也就是觀察因子的結構，ord.factor表示是排列的因子並有5個levels。後面的數字則是對應levels的排列，以上面來看1就是哥布林，2就是哈布林。