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. 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:

data <- data.frame(
  "A" = c(2, 4, NA, 8),
  "B" = c(5, NA, 7, 9),
  "C" = c(12, 13, 14, NA)
)

# Identify missing values
print(is.na(data))

Output is:

      A     B     C
[1,] FALSE FALSE FALSE
[2,] FALSE  TRUE FALSE
[3,]  TRUE FALSE FALSE
[4,] FALSE FALSE  TRUE

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 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:
library(tidyverse)

# Fill missing values with 0
data <- replace_na(data, list("A" = 0, "B" = 0, "C" = 0))
print(data)

Output is:

  A B  C
1 2 5 12
2 4 0 13
3 0 7 14
4 8 9  0

The missing values were replaced with 0.

  • Removing:
# Remove rows with missing values
data <- na.omit(data)
print(data)

Output is:

  A B  C
1 2 5 12

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.

Handling Missing Values in One Column
Real-World Implications
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!

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