Handling the Missing values
Handling the missing values into Data.csv
- Write following lines of code in R Script to handle missing values.
- What we are doing here
- Just placing an average of other observation into missing values.
- Here in Age & Salary column, we contain null value so we want to fill it with an average of other observation.
#handling missing values
dataset$Age = ifelse(
is.na(dataset$Age),
ave(dataset$Age,FUN = function(x) mean (x,na.rm=TRUE)),
dataset$Age
)
dataset$Salary = ifelse(
is.na(dataset$Salary),
ave(dataset$Salary,FUN = function(x) mean (x,na.rm=TRUE)),
dataset$Salary
)
- ifelse argument
- is.na(dataset$Age) gives a TRUE/FALSE value.
- the value if 1st argument returns TRUE, ave is used to perform average.
- the value if 1st argument return FALSE,dataset$Age
- After writing this line of code in R Script
- Press alt+enter
- See dataset data from Global Environment.
Comments
Post a Comment