Pivot Longer

In this lesson we will learn the opposite transformation to what we learned in the previous lesson. pivot_wider transformed our data from long to wide. pivot_longer will do the opposite, transform our data from wide to long.

As a recap, let's review the code introduced in the previous lesson when we converted the iris dataset from long to wide.

Next we will transform the data back from wide to long. The way to do this is actually quite similar to the way we transformed the data from long to wide. We will use the pivot_longer function, which takes as arguments:

  1. cols: This parameter specifies the columns to be moved from wide to long, in this case a vector of the column names: setosa, versicolor, and virginica.
  2. names_to: This parameter specifies the name to give to the new column that will be created to hold the group variable, in this case Species
  3. values_to: This parameter specifies the name to give to the new column that will be created to hold the values, in this case Petal.Length

Then we call the pivot_longer function on this dataframe, passing in the arguments as described above. Inspecting the data, we see that we have successfully transformed the data back to its original long form - although omitting the Petal.Length, Sepal.Length and Sepal.Width columns that were dropped in the transformation. We could keep those, but that problem is better kept for an advanced course in data science.

Note that in the code block on the right, we have included the iris_wide code from the previous prompt for you, so that does not need to be re-entered.

Practice exercise

This exercise continues the exercise from the previous lesson, so you can use your solution from there as the starting point. Use mutate, select and pivot_wider to transform the iris dataset from long to wide filling in the values from Petal.Width. Next map the data back to long form using pivot_longer.