R Function : Imputing Missing Values


R Data Science: R Programming A-Z: R For Data Science With Real Exercises!

The following is the R code for replacing missing values with mean, median, zero.

########################################################
# Imputing Missing Values with Mean / Median / Zero
#########################################################

#Replacing NA with zero
mydata[is.na(mydata)] <- 0

#Function: Imputing Missing Values with mean/median/min
impute <- function(data, type) {
  for (i in which(sapply(data, is.numeric))) {
    data[is.na(data[, i]), i] <- type(data[, i],  na.rm = TRUE)
  }
  return(data)}

#Implementing Imputation Function (Mean/Median/Min)
newdata <- impute(mydata,median)
Coursera Data Science

R Tutorials : 75 Free R Tutorials

Get Free Email Updates :
*Please confirm your email address by clicking on the link sent to your Email*

Related Posts:

0 Response to "R Function : Imputing Missing Values"

Post a Comment

Next → ← Prev