dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
sum(is.na(dirty_iris))
## [1] 58

Question 3

apply(is.na(dirty_iris), 2, sum)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##           10           17           19           12            0

Question 4

total_obs <- nrow(dirty_iris)

complete_obs <- sum(complete.cases(dirty_iris))

percent_complete <- (complete_obs / total_obs) * 100

complete_obs
## [1] 96
percent_complete
## [1] 64

Question 5

sapply(dirty_iris, function(x) {
  if (is.numeric(x)) sum(is.infinite(x)) else 0
})
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            1            0
sapply(dirty_iris, function(x) sum(is.na(x)))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##           10           17           19           12            0
# Check NaN
sapply(dirty_iris, function(x) if(is.numeric(x)) sum(is.nan(x)) else 0)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            0            0
# Check Inf or -Inf
sapply(dirty_iris, function(x) if(is.numeric(x)) sum(is.infinite(x)) else 0)
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            1            0
any(dirty_iris == Inf, na.rm = TRUE)
## [1] TRUE
any(dirty_iris == -Inf, na.rm = TRUE)
## [1] FALSE

Question 6

# Locate Inf
which(dirty_iris == Inf, arr.ind = TRUE)
##      row col
## [1,]  86   4
# Replace Inf with NA
dirty_iris[dirty_iris == Inf] <- NA

Question 7

violations <- subset(
  dirty_iris,
  Sepal.Width <= 0 | Sepal.Length > 30
)

violations
##     Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 16           5.0          -3          3.5         1.0 versicolor
## 28          73.0          29         63.0          NA  virginica
## 125         49.0          30         14.0         2.0     setosa
## 130          5.7           0          1.7         0.3     setosa
nrow(violations)
## [1] 4

Question 8

neg_width <- !is.na(dirty_iris$Sepal.Width) & dirty_iris$Sepal.Width < 0
zero_width <- !is.na(dirty_iris$Sepal.Width) & dirty_iris$Sepal.Width == 0

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

dirty_iris$Sepal.Width[zero_width] <- NA

Question 9

mean_sepal_width <- mean(dirty_iris$Sepal.Width, na.rm = TRUE)
dirty_iris$Sepal.Width[is.na(dirty_iris$Sepal.Width)] <- mean_sepal_width
median_petal_length <- median(dirty_iris$Petal.Length, na.rm = TRUE)
dirty_iris$Petal.Length[is.na(dirty_iris$Petal.Length)] <- median_petal_length
lm_model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width,
               data = dirty_iris, subset = !is.na(Sepal.Length))

missing_sl <- is.na(dirty_iris$Sepal.Length)
dirty_iris$Sepal.Length[missing_sl] <- predict(lm_model, newdata = dirty_iris[missing_sl, ])
library(VIM)
## Warning: package 'VIM' was built under R version 4.5.2
## 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
# Perform kNN imputation for Petal.Width only
dirty_iris <- kNN(dirty_iris, variable = "Petal.Width", k = 5, imp_var = FALSE)