始める無料で始める

タグ付き投稿の結合とカウント

前の演習で作成したテーブルは、questions_with_tagsanswers_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_tagsanswers_with_tags のテーブルを結合して posts_with_tags を作成します。
  • posts_with_tagsyear 列を追加し、typeyeartag_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 %>%
  ___
コードを編集して実行