Lesson 3
Mutate and Relocate
Welcome to Mutate and Relocate Functions

Welcome back! So far, you’ve learned how to select, rename, filter, and slice columns and rows using the powerful dplyr package in R. These skills help you clean and organize your data effectively. In this lesson, we’ll build on those skills by focusing on two more essential functions: mutate and relocate.

What You'll Learn

In this lesson, you’ll dive into the mutate and relocate functions in dplyr. These functions allow you to add new columns based on existing data and change the order of columns in your data frame. These capabilities are essential for enhancing and organizing your data for analysis.

We’ll use a sample data frame similar to what you’ve seen before:

R
1suppressPackageStartupMessages(library(dplyr)) 2 3# Sample dataframe 4sample_df <- tibble( 5 ID = 1:5, 6 Name = c("John", "Jane", "Alex", "Emily", "David"), 7 Age = c(28, 22, 35, 29, 40), 8 Salary = c(50000, 60000, 70000, 80000, 90000) 9) 10 11# Mutate to add new columns 12mutated_df <- sample_df %>% 13 mutate(Bonus = Salary * 0.1, TotalCompensation = Salary + Bonus) 14 15# Relocate the newly created TotalCompensation column after Salary 16final_df <- mutated_df %>% 17 relocate(TotalCompensation, .after = Salary)

You’ll learn how to:

  • Mutate: Add new columns based on existing data.
  • Relocate: Change the position of columns within your data frame.
Why It Matters

Manipulating data is a critical skill in data analysis and preparation. With these tools, you’ll be able to handle your data more flexibly and prepare it thoroughly for any analytical task. This step is crucial to making your data analysis as efficient and insightful as possible.

Ready to enhance your data manipulation skills even further? Let’s start the practice section and get hands-on with mutate and relocate.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.