Packages

library(VIM)
## Loading required package: colorspace
## Loading required package: grid
## VIM is ready to use.
## Suggestions and bug-reports can be submitted at: https://github.com/statistikat/VIM/issues
## 
## Attaching package: 'VIM'
## The following object is masked from 'package:datasets':
## 
##     sleep

Q3

dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")

sum(is.na(dirty_iris$Petal.Length))
## [1] 19

Q4

num_comp <- c(sum(complete.cases(dirty_iris)), mean(complete.cases(dirty_iris)) * 100)

num_comp
## [1] 96 64

Q5

colSums(is.na(dirty_iris))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##           10           17           19           12            0
sapply(dirty_iris, function(x) sum(is.infinite(x)))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            1            0
sapply(dirty_iris, function(x) sum(is.nan(x)))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            0            0
which(is.infinite(dirty_iris$Petal.Width))
## [1] 86

Q6

 dirty_iris$Petal.Width[is.infinite(dirty_iris$Petal.Width)] <- NA

dirty_iris$Petal.Width[86]
## [1] NA

Q7

violating_obs <- subset(dirty_iris, Sepal.Width <= 0 | Sepal.Length > 30)
nrow(violating_obs)
## [1] 4

Q8

violating_rows <- which(dirty_iris$Sepal.Width <= 0)

dirty_iris$Sepal.Width[which(dirty_iris$Sepal.Width < 0)] <-
  abs(dirty_iris$Sepal.Width[which(dirty_iris$Sepal.Width < 0)])

dirty_iris$Sepal.Width[which(dirty_iris$Sepal.Width == 0)] <- NA

dirty_iris$Sepal.Width
##   [1]  3.2  3.3   NA  3.4  2.6   NA  2.7  3.0  2.7  3.1  3.5  2.7  3.0  2.8  3.9
##  [16]  3.0   NA  3.2  4.0   NA  3.6   NA  2.8  3.3  3.0  3.2  3.1 29.0  3.2  2.8
##  [31]  3.2  3.2  2.8  2.9  2.9  3.0  3.0  2.2  2.5  3.0   NA  2.7   NA  2.7  4.2
##  [46]  2.8   NA  3.2  3.0  3.4  2.6  3.1  2.7  3.4  3.3  3.8  3.8  2.9  2.8  2.8
##  [61]  2.3  2.8  3.0  3.3  3.0  2.5  2.5  3.2  3.5  3.5  3.0  3.1  3.5   NA  2.8
##  [76]  2.5  3.5  3.0  3.8  3.8  2.6  3.4  2.9  3.7  3.0  3.8  2.9  2.9  2.9  2.5
##  [91]  3.2   NA  3.4  2.7  2.2  3.1  2.3   NA  3.0  2.8  3.4  3.6  2.7  3.0  3.7
## [106]   NA  3.0  3.0  2.8  3.4  3.4  3.4  3.4  3.3  3.1  2.6   NA  3.1  3.0  2.8
## [121]  3.0  2.3  3.2  4.1 30.0  2.9  3.2   NA  3.6   NA  2.5  3.1   NA  3.3  3.0
## [136]  3.0  3.2  3.0  3.1  2.2   NA   NA  3.0  2.9  2.5  3.1  3.0  3.5  3.1  2.6

Q9

# Sepal.Width
sw_mean <- mean(dirty_iris$Sepal.Width, na.rm = TRUE)
dirty_iris$Sepal.Width[is.na(dirty_iris$Sepal.Width)] <- sw_mean

#Petal.Length
pl_median <- median(dirty_iris$Petal.Length, na.rm = TRUE)
dirty_iris$Petal.Length[is.na(dirty_iris$Petal.Length)] <- pl_median

#Petal.Width
dirty_iris <- VIM::kNN(dirty_iris, variable = "Petal.Width", k = 5, imp_var = FALSE)
## Sepal.Length  Sepal.Width Petal.Length Sepal.Length  Sepal.Width Petal.Length 
##          0.0          2.2          0.0         73.0         30.0         63.0
#Sepal.Length
dirty_iris$Species <- as.factor(dirty_iris$Species)

fit <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width + Species,
          data = dirty_iris, subset = !is.na(Sepal.Length))

miss_sl <- which(is.na(dirty_iris$Sepal.Length))
if (length(miss_sl)) {
  dirty_iris$Sepal.Length[miss_sl] <- predict(fit, newdata = dirty_iris[miss_sl, ])
}

colSums(is.na(dirty_iris))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            0            0