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.
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'.
Missing values often appear as NA. The R language simplifies their detection using the is.na() function. This function returns a logical vector, which replaces 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.
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 missing values.
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 an error message. This message shows the versions of the core packages loaded and highlights any function name conflicts between packages. For these courses this conflicts is 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.
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!
