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:

  1. Mutating Data: Adding or transforming columns in your data frame using the mutate function.
  2. Arranging Data: Sorting your data in a specific order using the arrange function.

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:

# Example data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "David"),
  Score = c(85, 95, 78, 92)
)

# Print the example data frame
print(data)

# Output:
#      Name Score
# 1   Alice    85
# 2     Bob    95
# 3 Charlie    78
# 4   David    92

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.

# Example data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "David"),
  Score = c(85, 95, 78, 92)
)

# Add a new column
mutated_data <- mutate(data, ScorePlus10 = Score + 10)

# Print the mutated data
print(mutated_data)

# Output:
#      Name Score ScorePlus10
# 1   Alice    85          95
# 2     Bob    95         105
# 3 Charlie    78          88
# 4   David    92         102

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:

# Example data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "David"),
  Score = c(85, 95, 78, 92)
)

# Add a new column
mutated_data <- mutate(data, ScorePlus10 = Score + 10)

# Arrange data by Score in descending order
arranged_data <- arrange(mutated_data, desc(Score))

# Print the descending arranged data
print(arranged_data)

# Output:
#      Name Score ScorePlus10
# 1     Bob    95         105
# 2   David    92         102
# 3   Alice    85          95
# 4 Charlie    78          88

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:

# Example data frame
data <- data.frame(
  Name = c("Alice", "Bob", "Charlie", "David"),
  Score = c(85, 95, 78, 92)
)

# Add a new column
mutated_data <- mutate(data, ScorePlus10 = Score + 10)

# Arrange data by Score in ascending order
arranged_data_asc <- arrange(mutated_data, Score)

# Print the ascending arranged data
print(arranged_data_asc)

# Output:
#      Name Score ScorePlus10
# 1 Charlie    78          88
# 2   Alice    85          95
# 3   David    92         102
# 4     Bob    95         105

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.

Why It Matters

Mutating and arranging data are foundational skills in data wrangling.

  • Mutating Data: This technique allows you to create new variables or transform existing ones based on your needs. It's useful for tasks such as feature engineering in machine learning, where you may need to create new features from raw data.

  • Arranging Data: Sorting your data helps you see patterns more clearly and make your datasets more readable. For example, arranging sales data from the highest to the lowest can help you immediately spot your top-performing products.

By mastering these functions, you'll be better equipped to prepare your data for analysis and reporting, ensuring you draw more meaningful insights from your datasets.

Excited to start mutating and arranging data? Let's jump into the practice section and get hands-on with these powerful techniques.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal