最も一般的なレベルを残す
ときには比率ではなく、最もよく現れる n 個のレベルを残したいことがあります。MLMethodNextYearSelect について、比率ではなく数で残した場合にどのように残されるレベルが変わるか見てみましょう。multiple_choice_responses はあらかじめ読み込まれています。
この演習はコースの一部です
tidyverse で学ぶカテゴリ型データ
演習の手順
- メソッドを選択していない人を除外します。
MLMethodNextYearSelectから新しい変数ml_methodを作成し、最も一般的なタイトルを5つ残し、残りは引数other_levelを使って "other method" にまとめます。- 各
ml_methodの出現頻度を数え、降順に並べ替えます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
multiple_choice_responses %>%
# Remove NAs
filter(___) %>%
# Create ml_method, retaining the 5 most common methods and renaming others "other method"
mutate(ml_method = ___(MLMethodNextYearSelect, ___, other_level = ___)) %>%
# Count the frequency of your new variable, sorted in descending order
___(ml_method, ___)