R Function : Imputing Missing Values

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)

Best Online Course : Practical Data Science using R

- Explain Advanced Algorithms in Simple English
- Live Projects & Case Studies
- Domain Knowledge
- Job Placement Assistance
- Money Back Guarantee


R Tutorials : 75 Free R Tutorials

About Author:

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has close to 7 years of experience in data science and predictive modeling. During his tenure, he has worked with global clients in various domains like retail and commercial banking, Telecom, HR and Automotive.


While I love having friends who agree, I only learn from those who don't.

Let's Get Connected: Email | LinkedIn

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