Session Ready
Exercise

Rename and subset variables

In the next chapter, when we reshape data to be tidy, you'll see the importance of having tame variable names.

Now that our variable names in ratings have been converted to snake_case, you'll practice combining renaming with select() helper functions to take advantage of the variable name structure.

As mentioned in the video, to rename a group of variables, you only need to specify a prefix of the new name.

For example, my_tbl %>% select( new_name_ = starts_with("oldname") ) will find all variables in my_tbl whose names start with oldname, enumerate them, then rename each variable as new_name_<N>, where N is a number. If my_tbl has variables oldname, oldname_v1, oldname3, running the statement above will replace these names with new_name_1, new_name_2, new_name_3. Notice how we only specified new_name_ in select(), and the numbers were appended automatically!

Take a second to glimpse() at ratings in your console, and take note of the variables that store data about the number of viewers. Can you see the pattern in their names? In this exercise, you will first select those variables using a helper function, and then rename them!

Instructions 1/2
undefined XP
  • 1
  • 2
  • Select series and the variables with 7-day viewer data from ratings. Use a helper function inside select() to match the naming pattern of the variables that contain the viewer data;
  • glimpse() to view the result.