Topic Overview and Actualization

In today's lesson, we will focus on identifying and handling duplicates and outliers to clean our dataset for a more precise analysis.

R Tools for Handling Duplicates

Consider a dataset containing students' details from a school. If a student's information is repeated in the dataset, we classify that as a duplicate. Duplicates can distort our data, leading to inaccurate results during the analysis.

# Create DataFrame
df <- data.frame(
    Name = c('John', 'Anna', 'Peter', 'John', 'Anna'), 
    Age =  c(16, 15, 13, 16, 15), 
    Grade = c(9, 10, 7, 9, 10)
)

R provides efficient functionalities to handle duplicates in a dataset. Here's how you can identify duplicates:

# Identify duplicates
print(df[duplicated(df),])
# 4 John  16     9
# 5 Anna  15    10

The duplicated() function in R flags duplicate rows. This function can also be used to remove duplicate rows:

# Remove duplicates
df <- df[!duplicated(df),]
print(df)

After removing the duplicates, your data is clean and ready!

  Name Age Grade
1  John  16     9
2  Anna  15    10
3 Peter  13     7
Identifying Outliers

An outlier is a data point that is anomalously different from other data points in the same dataset. For instance, in our dataset of primary school students' ages, discovering an age like 98 would be considered an outlier.

Outliers can be detected visually using tools like box plots and scatter plots, or even through statistical methods such as the Z-score or IQR. Today, we will use the IQR method to detect outliers:

Here's a brief reminder: a value is considered an outlier if it is at least 1.5 * IQR less than Q1 (first quartile) or at least 1.5 * IQR greater than Q3 (third quartile).

R Tools for Handling Outliers
Handling Outliers: Removal
Handling Outliers: Replacement
Handling Outliers: Replacement with Mean
Summary

This lesson discussed what duplicates and outliers are, their implications on data analysis, and how to handle them using R. The key to accurate data analysis is clean data. Now is the best time to apply these concepts to real-world data! Let's dive into some practical exercises!

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