Session Ready
Exercise

Unite columns

In the tidyr package, the opposite of separate() is unite(). Sometimes you need to paste values from two or more columns together to tidy. Here is an example usage for unite():

data %>%
    unite(new_var, old_var1, old_var2)

The ratings2 dataset includes these variables:

  • series: 1-8
  • episode: 1-10
  • viewers_millions: whole number of 7-day viewers in millions
  • viewers_decimal: additional number of 7-days viewers in millions

If viewers_millions = 2 and viewers_decimal = .6, then the total number of viewers in millions is 2.6. In this exercise, you'll practice uniting these two columns into a single new column.

Instructions 1/3
undefined XP
  • 1
    • Create a new variable by uniting the two viewers data columns into one column called viewers_7day; print to view.
    • 2
      • Adapt your unite() code to use a new argument, sep. The default separator between new columns is _. Instead, set sep = "". Print the new data frame to view it.
    • 3
      • Adapt the provided code to cast the new viewers_7day column to a number.