彙總組合以比較其差異
若要比較兩個特定組合,以及組成它們的 LEGO 零件種類,我們需要先把資料依不同主題彙總。此外,如同影片所示,我們還需要新增一個欄位,讓你能看出各組合中某些零件所占的比例,而不是只看零件數量本身。
inventory_parts_themes 資料表已為你預先載入。
inventory_parts_themes <- inventories %>%
inner_join(inventory_parts, by = c("id" = "inventory_id")) %>%
arrange(desc(quantity)) %>%
select(-id, -version) %>%
inner_join(sets, by = "set_num") %>%
inner_join(themes, by = c("theme_id" = "id"), suffix = c("_set", "_theme"))
本練習屬於課程
使用 dplyr 進行資料表連接
練習說明
- 加上
"Batman"主題的篩選,建立batman_colors物件。 - 在
batman_colors中新增fraction欄位,顯示 total 除以 total 總和的結果。 - 依樣的步驟篩選並彙總
"Star Wars"組合資料,建立star_wars_colors物件。 - 在
star_wars_colors中新增fraction欄位,顯示 total 所占的比例。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
batman_colors <- inventory_parts_themes %>%
# Filter the inventory_parts_themes table for the Batman theme
filter(name_theme == ___) %>%
group_by(color_id) %>%
summarize(total = sum(quantity)) %>%
# Add a fraction column of the total divided by the sum of the total
mutate(___)
# Filter and aggregate the Star Wars set data; add a fraction column
star_wars_colors <- inventory_parts_themes %>%
___