Aggregating sets to look at their differences
To compare two individual sets, and the kinds of LEGO pieces that comprise them, we'll need to aggregate the data into separate themes. Additionally, as we saw in the video, we'll want to add a column so that we can understand the fractions of specific pieces that are part of each set, rather than looking at the numbers of pieces alone.
The inventory_parts_themes
table has been preloaded for you.
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"))
This exercise is part of the course
Joining Data with dplyr
Exercise instructions
- Add a filter for the
"Batman"
theme to create thebatman_colors
object. - Add a
fraction
column tobatman_colors
that displays the total divided by the sum of the total. - Repeat the steps to filter and aggregate the
"Star Wars"
set data to create thestar_wars_colors
object. - Add a
fraction
column tostar_wars_colors
to display the fraction of the total.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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 %>%
___