Introduction

This Code-Through tutorial demonstrates how to clean, manipulate, and explore a dataset in R using the dplyr package. The goal is to provide a simple, step-by-step walkthrough that a beginner can follow to understand the essential data-wrangling workflow.

Using a small student performance dataset, this tutorial explains how to:

Inspect and understand raw data Use dplyr verbs such as filter(), mutate(), select(), and arrange() Create new variables to support analysis Clean incomplete or inconsistent observations Summarize results Visualize the cleaned dataset using ggplot2

By the end, readers should feel confident performing basic data cleaning operations in R and understanding why each step matters.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

Before cleaning, we examine the raw data to understand its structure and identify issues.

students_raw <- data.frame(
Name = c("Alice", "Bob", "Charlie", "Diana", "Ethan"),
Math_Score = c(85, NA, 92, 70, 65),
Science_Score = c(88, 75, 95, NA, 72),
Gender = c("F", "M", "M", "F", "M")
)

students_raw

The initial dataset contains missing values and inconsistent information that need attention.

Cleaning the Dataset We remove rows with missing values to ensure the dataset is ready for analysis.

students_clean <- students_raw %>%
filter(!is.na(Math_Score), !is.na(Science_Score))

students_clean

This results in a smaller, fully complete dataset suitable for reliable calculation.

Creating New Variables Next, we compute a new column called Average_Score, which helps summarize each student’s performance across subjects.

students_clean <- students_clean %>%
mutate(Average_Score = (Math_Score + Science_Score) / 2)

students_clean

This makes it easier to compare students directly.

Arranging the Dataset We now sort students in descending order by their average score.

students_sorted <- students_clean %>%
arrange(desc(Average_Score))

students_sorted

This immediately reveals the top-performing students.

Final Clean Dataset

students_sorted

This is the cleaned, structured dataset ready for analysis or reporting.

Visualization To better understand the pattern in average scores, we visualize the results using a bar chart.

ggplot(students_sorted, aes(x = Name, y = Average_Score, fill = Gender)) +
geom_bar(stat = "identity") +
labs(
title = "Average Score by Student",
x = "Student Name",
y = "Average Score"
) +
scale_fill_manual(values = c("F" = "purple", "M" = "orange")) +
theme_minimal()

The plot visually highlights the performance differences and makes the results easy to interpret.

Conclusion

This Code-Through demonstrated how to clean, transform, and visualize data using the dplyr and ggplot2 packages in R. By working through a small example dataset, we applied essential steps such as removing incomplete observations, creating new variables, arranging data for interpretation, and generating a plot to summarize insights.

These foundational techniques form the core of most data-wrangling workflows and can be applied to any dataset. The workflow shown here equips beginners with the confidence to perform their own data cleaning tasks and prepare data for deeper statistical analysis.