タグ付き投稿の結合とカウント
前の演習で作成したテーブルは、questions_with_tags と answers_with_tags として読み込まれています。まず、これらを posts_with_tags という1つのテーブルに結合しましょう。1つのテーブルに情報をまとめたら、lubridate パッケージ(すでに読み込み済み)を使って日付変数を作成し、さらに情報を追加できます。
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"))
この演習はコースの一部です
dplyr で行うデータの結合
演習の手順
questions_with_tagsとanswers_with_tagsのテーブルを結合してposts_with_tagsを作成します。posts_with_tagsにyear列を追加し、type、year、tag_nameごとに投稿数を集計します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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 %>%
___