Data Manipulation and Transformation

Introduction to Data Manipulation and Transformation

Welcome back! Before we dive into the specifics of manipulating and transforming your data in R, let's recap what we've covered so far. We started with essential R programming concepts, then moved on to acquiring and preparing data for analysis. Now, it's time to take those clean datasets and learn how to manipulate and transform them effectively to uncover insights.

What You'll Learn

In this lesson, you will master various data manipulation and transformation techniques using the powerful dplyr library. Specifically, you'll learn how to:

  1. Subset Your Data: Extract specific columns and filter rows based on conditions to focus on the most relevant parts of your dataset.
  2. Transform Data: Modify existing variables and create new ones to enrich your dataset with additional useful information.
  3. Aggregate Data: Summarize and group data to generate meaningful statistics and understand patterns within your dataset.

Here's a step-by-step breakdown of the techniques you'll be practicing:

Create a Sample Data Frame

We'll create a sample data frame to demonstrate various data manipulation techniques.

# Create a sample data frame
df <- data.frame(
  ID = 1:5,
  Name = c("John", "Jane", "Doe", "Smith", "Emily"),
  Score = c(85, 90, 88, 77, 95)
)

Subsetting Data

Subsetting allows you to focus on specific columns or rows in your dataset. Here, we'll use the dplyr library to select the Name and Score columns.

# Subsetting Data: Select Name and Score columns
selected_data <- dplyr::select(df, Name, Score)
print("Selected Data")
print(selected_data)

The select function from dplyr is used to extract specific columns from the data frame. In this case, we are selecting the Name and Score columns.

Syntax of select function:

  • select(data_frame, column1, column2, ...)
    • data_frame: The data frame from which you want to select columns.
    • column1, column2, ...: The names of the columns you want to select.

Filtering Data

Filtering helps you keep only the rows that meet certain conditions. In this example, we'll filter the rows where the Score is greater than 80.

# Filtering Data: Keep rows where Score is greater than 80
filtered_data <- dplyr::filter(df, Score > 80)
print("Filtered Data")
print(filtered_data)

The filter function from dplyr is used to extract rows that meet a specified condition. We are keeping rows where the Score is greater than 80.

Syntax of filter function:

  • filter(data_frame, condition)
    • data_frame: The data frame from which you want to filter rows.
    • condition: The condition that the rows must meet to be included in the output.
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