Identifying and Handling Missing Values in R Data Cleaning Process
Introduction and Overview
Welcome! Today's focus is 'Identifying and Handling Missing Values,' a crucial step in data cleansing that ensures the completeness of our dataset. Essential for reliable analysis, we'll delve into the complexities of finding and handling missing values.
The Art of Data Cleaning
Imagine untangling a pile of necklaces; it's a tedious but necessary task for utilizing each piece. Likewise, datasets may contain chaos, such as misspellings, incorrect data types, and even missing values, all of which need order. This sorting process is known as 'Data Cleaning'.
Identifying Missing Values
Missing values often appear as NA. The R language simplifies their detection using the is.na() function. For a data frame, this function returns a logical matrix, which marks missing values with TRUE and non-missing values with FALSE.
Let's examine this functionality using a small dataset:
Output is:
Using this, we've identified the missing values.
Handling Missing Values
After identifying missing values, R language provides several strategies to handle them. We will use a tidyverse library for it. It has the following functions:
replace_na(): Replaces the missing values.na.omit(): Removes the rows with missing values. It is part of base R, so it works withouttidyverse.
Note that both functions return a new DataFrame. If you want to update the original data, you'll need to re-assign it
Let's apply these strategies:
- Replacing:
Output is:
The missing values were replaced with 0.
- Removing:
Output is:
In the latter example, all rows with NA were deleted, leaving us with just one row.
Note that when you load the tidyverse library, you get a startup message. This message shows the versions of the core packages loaded and highlights any function name conflicts between packages. For these courses these conflicts are not a problem, so you may ignore it. But generally a systematic way to handle conflicts is to use the conflicted library. However, we won't cover it in this particular course.
Handling Missing Values in One Column
The replace_na() function applies to the entire dataset, which might not always be ideal. In most instances, you'll want to handle missing values in specific columns individually. In an R dataframe df, columns can be accessed like list elements:
Output is:
Now, there are no more missing values in the "A" column.
Real-World Implications
Missing values in real-world datasets are inevitable. Whether it's a company's financial data or a hospital's patient records, missing values are present and need to be properly managed, as they significantly affect the outcome of our analysis.
A common technique for filling missing values is to use the average value. Here's an example using incomplete age values:
Output is:
In the above example, we first create a dataframe with names and ages, where some age values are missing (represented as NA in the dataframe). To fill the missing age values with the mean age, we use the replace_na() function, with the parameter being mean(data$age, na.rm = TRUE). The mean() function ignores missing values (i.e., NAs) when the na.rm parameter is set to TRUE; hence, it works correctly without any workarounds.
Lesson Summary
Congratulations! You've learned how to identify and handle missing values using R. Now, prepare for some hands-on exercises to apply these concepts to various datasets. This lesson is an opportunity to solidify your understanding and refine your skills in managing missing values. Happy coding!
