dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
# Number of complete observations
num_complete <- sum(complete.cases(dirty_iris))
num_complete
## [1] 96
# Percentage of complete observations
pct_complete <- mean(complete.cases(dirty_iris)) * 100
pct_complete
## [1] 64

#Question 6

which(is.infinite(dirty_iris$Petal.Width))
## [1] 86
dirty_iris$Petal.Width[is.infinite(dirty_iris$Petal.Width)] <- NA

#Question 8

which(dirty_iris$Sepal.Width <= 0)
## [1]  16 130
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

#Question 9

# Set Sepal.Length > 30 to NA first
dirty_iris$Sepal.Length[which(dirty_iris$Sepal.Length > 30)] <- NA

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

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

# 3. Sepal.Length: linear regression
model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Species, data = dirty_iris)
dirty_iris$Sepal.Length[is.na(dirty_iris$Sepal.Length)] <- predict(model, dirty_iris[is.na(dirty_iris$Sepal.Length), ])

# 4. Petal.Width: kNN (k = 5, base R)
na_pw <- which(is.na(dirty_iris$Petal.Width))
ok_pw <- which(!is.na(dirty_iris$Petal.Width))
d <- as.matrix(dist(scale(dirty_iris[, 1:3])))

dirty_iris$Petal.Width[na_pw] <- sapply(na_pw, function(i) {
  mean(dirty_iris$Petal.Width[ok_pw[order(d[i, ok_pw])[1:5]]])
})

# Check that 0 NAs remain
colSums(is.na(dirty_iris))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            0            0