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.
After removing the duplicates, your data is clean and ready!
Name Age Grade1 John 16 92 Anna 15 103 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!
There are generally two strategies for dealing with outliers — removing them or replacing them with a median value.
Removing outliers is the most straightforward method. However, you might opt for other methods as removing outliers can result in data loss. To apply it, let's reverse the condition to choose everything except outliers.
Alternatively, outliers can be replaced with median values. The median value is less susceptible to outliers and hence suitable for replacement.
R
# Replace outliers with median scoresmedian_score <- median(df$scores)df$scores[df$scores < lower_bound | df$scores > upper_bound] <- median_scoreprint(df)
Here, we select outliers using boolean selection and make them equal to the median score. The median is 50, hence outlier scores are replaced with 50:
students scores1 Alice 562 Bob 503 John 504 Ann 505 Rob 47
An alternative to replacing outliers with the median is using the dataset's mean, excluding the outliers. This method ensures that the replacement value reflects the central tendency of the main distribution of data without being skewed by the extreme values.
First, we need to calculate the mean of the data, excluding the outliers:
R
# Calculating mean without outliersmean_scores <- mean(df$scores[(df$scores >= lower_bound & df$scores <= upper_bound)])
This approach replaces outliers with a mean score that is representative of the bulk of the data, ensuring a more balanced dataset:
students scores1 Alice 56.02 Bob 51.03 John 50.04 Ann 51.05 Rob 47.0
Note that the mean value 51 (rounded for simplicity) is calculated without the outliers, offering a more accurate depiction of the central value of most data points.