Gộp và đếm bài viết theo thẻ (tags)
Các bảng bạn đã tạo ở bài trước đã được nạp sẵn dưới tên questions_with_tags và answers_with_tags. Trước tiên, bạn sẽ gộp hai bảng này thành một bảng duy nhất gọi là posts_with_tags. Khi đã tập hợp thông tin vào một bảng, bạn có thể bổ sung thêm bằng cách tạo biến ngày dùng gói lubridate, gói này đã được nạp sẵn cho bạn.
questions_with_tags <- questions %>%
inner_join(question_tags, by = c("id" = "question_id")) %>%
inner_join(tags, by = c("tag_id" = "id"))
answers_with_tags <- answers %>%
inner_join(question_tags, by = "question_id") %>%
inner_join(tags, by = c("tag_id" = "id"))
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
- Gộp hai bảng
questions_with_tagsvàanswers_with_tagsthànhposts_with_tags. - Thêm cột
yearvào bảngposts_with_tags, rồi đếm số bài theotype,year, vàtag_name.
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.
# Combine the two tables into posts_with_tags
posts_with_tags <- ___(questions_with_tags %>% mutate(type = "question"),
answers_with_tags %>% mutate(type = "answer"))
# Add a year column, then count by type, year, and tag_name
posts_with_tags %>%
___