Get startedGet started for free

Handling missing data

Some of the prospective donors have missing age data. Unfortunately, R will exclude any cases with NA values when building a regression model.

One workaround is to replace, or impute, the missing values with an estimated value. After doing so, you may also create a missing data indicator to model the possibility that cases with missing data are different in some way from those without.

The data frame donors is loaded in your workspace.

This exercise is part of the course

Supervised Learning in R: Classification

View Course

Exercise instructions

  • Use summary() on donors$age to find the average age of prospects with non-missing data.
  • Use ifelse() and the test is.na(donors$age) to impute the average (rounded to 2 decimal places) for cases with missing age. Be sure to also ignore NAs.
  • Create a binary dummy variable named missing_age indicating the presence of missing data using another ifelse() call and the same test.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Find the average age among non-missing values
summary(___)

# Impute missing age values with the mean age
donors$imputed_age <- ifelse(___)

# Create missing value indicator for age
donors$missing_age <- ___
Edit and Run Code