DAY 8
0
AI & Data

## [Data Science系列]R語言多維空間的管理-解析分類人物屬性值

"但如果能將資料全部統和在一起，是不是更方便管理呢？"恩君疑惑的問道
"沒問題，這時我們就可以運用list列表，將所有的數值都儲存起來"

list和其他vector及matrix不同之處在於list內不會被強制轉成同一類no coercion
list內的各元素類型是可以為任意對象，換句話說不同的元素是可以存在list裡的

``````解析村長 <- c("村長", "Goblin", 302, "RICH")

[1] "村長"   "Goblin" "302"    "RICH"
``````

``````str(解析村長)
chr [1:4] "村長" "Goblin" "302" "RICH"
``````

``````list("村長", "Goblin", 302, "RICH")
[[1]]
[1] "村長"

[[2]]
[1] "Goblin"

[[3]]
[1] 302

[[4]]
[1] "RICH"
``````

``````names(解析村長) <- c("名字", "種族", "儲蓄", "富有度")

\$名字
[1] "村長"

\$種族
[1] "Goblin"

\$儲蓄
[1] 302

\$富有度
[1] "RICH"
``````

``````str(解析村長)
List of 4
\$ 名字    : chr "村長"
\$ 種族    : chr "Goblin"
\$ 儲蓄    : num 302
\$ 富有度: chr "RICH"
``````

``````將數值存於na中
na <- c("Billy", "Bloodhoof", "Scars", "Dean", "Sammy")
``````

``````ra代表種族
ra <- c("Orc", "Hoblin", "Goblin", "Elf", "Harpy")
``````

``````sa代表savings
sa <- c(120, 88, 50, 190, 166)
``````

``````wealth <- factor(c("OK", "POOR", "POOR", "RICH", "RICH" ), ordered = TRUE, levels = c("POOR", "OK", "RICH"))
wealth
[1] OK   POOR POOR RICH RICH
Levels: POOR < OK < RICH
``````

``````村莊 <- list(title = "村莊人口狀況", "名字" = na, "種族" = ra, "儲蓄" = sa, "富有度" = we )
``````

``````str(村莊)
List of 5
\$ title : chr "村莊人口狀況"
\$ 名字  : chr [1:5] "Billy" "Bloodhoof" "Scars" "Dean" ...
\$ 種族  : chr [1:5] "Orc" "Hoblin" "Goblin" "Elf" ...
\$ 儲蓄  : num [1:5] 120 88 50 190 166
\$ 富有度: Ord.factor w/ 3 levels "POOR"<"OK"<"RICH": 2 1 1 3 3
``````