DAY 3
0
AI & Data

## [Data Science系列]運用R語言進行分配-哥布林恩家中分配食物

OK!這次恩君透過R語言了解到了運用簡易加減來記錄上禮拜零用錢，賭博，及其他花費～

"恩君，村長最近告知咱們村鬧飢荒阿，在食物資源來之前要好好規劃食物分配阿"
"阿～我們家有6個兄弟姊妹耶，該盤點一下食物量了，但一人該要幾份呢？"

"沒問題的，恩君還記得昨天零用金的計算方式嗎?讓我們來試試看"

``````food_vector <- c(apple = 60 , orange = 48 , kiwi = 30 , banana = 42 , watermelon = 12, fish = 18, pork = 36 , cheese = 6)
food_vector
apple     orange       kiwi     banana watermelon       fish       pork     cheese
60         48         30         42         12         18         36          6
``````

"酷!"
"接著家裡的兄弟都能分到幾個呢？"

``````each_one <- food_vector/6
apple     orange       kiwi     banana watermelon       fish       pork     cheese
10          8          5          7          2          3          6          1
``````

"嘿～恩君，等等，食物數量有誤阿，記得魚肉和豬肉上次就吃光了阿"
"糟糕～那該怎麼算呢"
"沒問題的，這時透過子集(subseting)可以簡單選擇或剔除不要的數值"

``````each_one [c(1, 2, 3, 4, 5)]
apple     orange       kiwi     banana watermelon     cheese
10          8          5          7          2          1
``````

``````each_one [c(1:5)]
apple     orange       kiwi     banana watermelon     cheese
10          8          5          7          2          1
``````

``````each_one [c(-6, -7)]
apple     orange       kiwi     banana watermelon     cheese
10          8          5          7          2          1
``````

"可是這樣的話恩君還要去數第幾個代表的食物，難道不能直接用食物的名字來看數量嗎？"
"就知道你會問"

``````each_one[c("kiwi", "apple", "orange")]
kiwi  apple orange
5     10      8
``````

``````each_one[kiwi]
kiwi
5
``````