Lesson 4 of 4

Missing Value Treatment

In Lesson 3 you met one blank cell, the unlogged Bagel sale, and patched it with na.rm = TRUE. Real data is never that tidy. Maya ran a quick exit survey one busy morning, twelve customers, five questions each, and came back with a table full of holes: a spend she never recorded, a rating the tablet ate, tips people simply would not say.

The wrong move is to delete every row with a blank and carry on. As you will see, that throws away two thirds of Maya's survey and quietly biases what is left. This lesson is about treating those holes honestly.

By the end you will be able to:

  • Find and count missing values, and see how far the damage spreads
  • Name why data goes missing (the three mechanisms: MCAR, MAR, MNAR)
  • Choose between dropping and filling, and say how each choice bends the answer

Prerequisites: Lessons 1 to 3 of this course, so you know tidy data, the dplyr verbs and the pipe, and group_by / summarise with na.rm. Everything new is defined as it appears.

First, see it

Find the holes before you fix them

Here is Maya's survey. Each lesson starts in a fresh R session, so we build the table right here (run this once). A blank answer is stored as NA, which R reads as "a value exists but I do not know it". One column carries each kind of hole, and we will return to that on purpose.

RInteractive R
library(dplyr) # the verbs and the pipe library(tibble) # tibble() survey <- tibble( customer = c("Ana","Ben","Cara","Dan","Eve","Finn","Gus","Hana","Ivy","Jon","Kim","Leo"), payment = c("card","cash","card","card","cash","card","card","cash","card","cash","card","card"), spend = c( 12, NA, 18, 9, NA, 25, 14, 8, 11, NA, 30, 16), rating = c( 5, 4, NA, 4, 5, 4, 2, NA, 5, 3, 4, 3), tip = c( 3, 2, 4, NA, 5, 6, NA, 3, 4, NA, 8, NA) ) survey #> # A tibble: 12 x 5 #> customer payment spend rating tip #> <chr> <chr> <dbl> <dbl> <dbl> #> 1 Ana card 12 5 3 #> 2 Ben cash NA 4 2 #> 3 Cara card 18 NA 4 #> 4 Dan card 9 4 NA #> 5 Eve cash NA 5 5 #> 6 Finn card 25 4 6 #> 7 Gus card 14 2 NA #> 8 Hana cash 8 NA 3 #> 9 Ivy card 11 5 4 #> 10 Jon cash NA 3 NA #> 11 Kim card 30 4 8 #> 12 Leo card 16 3 NA

  

is.na(x) is the workhorse: it returns TRUE wherever a value is missing. Wrap it in sum() to count blanks, and colSums() to count them per column:

RInteractive R
colSums(is.na(survey)) #> customer payment spend rating tip #> 0 0 3 2 4

  

So nine cells are blank: three spends, two ratings, four tips. The dplyr way says the same thing, one verb per column:

RInteractive R
survey %>% summarise(across(everything(), ~ sum(is.na(.)))) #> # A tibble: 1 x 5 #> customer payment spend rating tip #> <int> <int> <int> <int> <int> #> 1 0 0 3 2 4

  

Counting per column hides something, though. The real question is how many whole rows are usable. complete.cases() flags the rows with no blank anywhere:

RInteractive R
sum(complete.cases(survey)) # rows with zero missing values #> [1] 4 mean(complete.cases(survey)) # ... as a share of all 12 rows #> [1] 0.3333333

  

Only four of the twelve customers, Ana, Finn, Ivy and Kim, answered every question. Nine scattered blanks have spoiled two thirds of the rows.