การรวมข้อมูลชุดต่างๆ เพื่อดูความแตกต่าง
เพื่อเปรียบเทียบชุด 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"))
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
การ Join ข้อมูลด้วย dplyr
คำแนะนำการฝึกหัด
- เพิ่มตัวกรองสำหรับธีม
"Batman"เพื่อสร้างออบเจกต์batman_colors - เพิ่มคอลัมน์
fractionในbatman_colorsที่แสดงผลรวมหารด้วยผลรวมทั้งหมด - ทำซ้ำขั้นตอนเดิมเพื่อกรองและรวมข้อมูลชุด
"Star Wars"แล้วสร้างออบเจกต์star_wars_colors - เพิ่มคอลัมน์
fractionในstar_wars_colorsเพื่อแสดงสัดส่วนของผลรวมทั้งหมด
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
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 %>%
___