始める無料で始める

セットを集計して違いを見てみましょう

2つの個別のセットと、それらを構成する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 オブジェクトを作成します。
  • 合計をその合計の総和で割った値を表示する fraction 列を batman_colors に追加します。
  • 同じ手順で "Star Wars" セットのデータをフィルター・集計して、star_wars_colors オブジェクトを作成します。
  • star_wars_colors にも、合計に対する割合を表示する fraction 列を追加します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

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 %>%
  ___
	

コードを編集して実行