Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go


Missing Values

20180726 Missing values for the variables are an issue for some but not all algorithms. For example randomForest::randomForest() omits observations with missing values by default whilst rpart::rpart() has a particularly well developed approach to dealing with missing values.

We may want to impute missing values in the data (though it is not always wise to do so). Here we do this using randomForest::na.roughfix() from randomForest. This function provides, as the name implies, a rather basic algorithm for imputing missing values. Because of this we will demonstrate the process but then restore the original dataset—we will not want this imputation to be included in our actual dataset.

# Backup the dataset so we can restore it as required.

ods <- ds

# Count the number of missing values.

ds[vars] %>%  is.na() %>% sum()
## [1] 399165

# Impute missing values.

ds[vars] %<>% na.roughfix()

# Confirm that no missing values remain.

ds[vars] %>%  is.na() %>% sum()
## [1] 0

As foreshadowed we now restore the dataset with its original contents.

# Restore the original dataset.

ds <- ods

Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.