Mutating and Arranging Data
Mutating and Arranging Data
Welcome back! Up until now, we've covered some essential techniques for managing your datasets with the dplyr package in R. We've learned how to select specific columns, filter rows based on conditions, and summarize and group data. Now, it's time to take your data manipulation skills to the next level by learning how to mutate (or transform) data and arrange it in a specific order.
What You'll Learn
In this lesson, you'll explore two critical functionalities:
- Mutating Data: Adding or transforming columns in your data frame using the
mutatefunction. - Arranging Data: Sorting your data in a specific order using the
arrangefunction.
We'll use straightforward examples to make these concepts easy to grasp. Let’s dive into each of these functionalities.
Example Data Frame
First, we'll set up an example data frame that we'll use throughout this lesson:
This data frame contains the names of four individuals along with their respective scores.
Adding a New Column with mutate
The mutate function allows us to add new columns or transform existing ones. For instance, suppose we want to add a new column, ScorePlus10, which is each person's score incremented by 10.
Here, mutate adds a new column called ScorePlus10 to the data frame, where each entry is the original Score plus 10.
Arranging Data with arrange
The arrange function helps us sort the data in a specific order. For example, to sort the data by Score in descending order, we can do the following:
In this code snippet, arrange sorts the mutated_data data frame by the Score column in descending order.
To sort the data by Score in ascending order, we can do the following:
Here, arrange sorts the mutated_data data frame by the Score column in ascending order. No need to use any function for ascending order, as it is the default behavior of arrange.
