Gộp (aggregate) các bộ để xem sự khác biệt của chúng
Để so sánh hai bộ (set) riêng lẻ và các loại mảnh LEGO cấu thành chúng, bạn sẽ cần gộp dữ liệu theo từng chủ đề (theme) riêng. Ngoài ra, như đã thấy trong video, bạn sẽ muốn thêm một cột để tính tỷ phần (fraction) của các mảnh cụ thể thuộc mỗi bộ, thay vì chỉ nhìn vào số lượng mảnh.
Bảng inventory_parts_themes đã được nạp sẵn cho bạn.
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"))
Bài tập này là một phần của khóa học
Kết hợp dữ liệu với dplyr
Hướng dẫn bài tập
- Thêm bộ lọc cho chủ đề
"Batman"để tạo đối tượngbatman_colors. - Thêm một cột
fractionvàobatman_colorshiển thị tổng chia cho tổng của toàn bộ (sum of the total). - Lặp lại các bước lọc và gộp cho dữ liệu bộ
"Star Wars"để tạo đối tượngstar_wars_colors. - Thêm một cột
fractionvàostar_wars_colorsđể hiển thị tỷ phần của tổng.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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 %>%
___