# install.packages("VIM")   # run once if VIM isn't installed
library(VIM)   # for kNN imputation

Question 1 – Technically correct data

Answer: B – technically correct data. Each value is recognizable as belonging to a variable and is stored in a data type that fits that variable’s real-world domain.

Question 2 – Consistent data

Answer: C – consistent data. The data is technically correct, and its missing values, special values, errors, and outliers have been handled so it fits real-world constraints.

Question 3 – Missing values in Petal.Length

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

str(dirty_iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  6.4 6.3 6.2 5 5.7 5.3 6.4 5.9 5.8 4.8 ...
##  $ Sepal.Width : num  3.2 3.3 NA 3.4 2.6 NA 2.7 3 2.7 3.1 ...
##  $ Petal.Length: num  4.5 6 5.4 1.6 3.5 NA 5.3 5.1 4.1 1.6 ...
##  $ Petal.Width : num  1.5 2.5 2.3 0.4 1 0.2 NA 1.8 1 0.2 ...
##  $ Species     : chr  "versicolor" "virginica" "virginica" "setosa" ...
colSums(is.na(dirty_iris))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##           10           17           19           12            0
sum(is.na(dirty_iris$Petal.Length))
## [1] 19

Answer: D – 19.

Question 4 – Complete observations

# Option 1
num_complete <- sum(complete.cases(dirty_iris))
num_complete
## [1] 96
# Option 2
nrow(na.omit(dirty_iris))
## [1] 96
pct_complete <- num_complete / nrow(dirty_iris) * 100
pct_complete
## [1] 64

Answer: B – 96 observations, 64%.

Question 5 – Other special values

summary(dirty_iris)
##   Sepal.Length     Sepal.Width      Petal.Length    Petal.Width 
##  Min.   : 0.000   Min.   :-3.000   Min.   : 0.00   Min.   :0.1  
##  1st Qu.: 5.100   1st Qu.: 2.800   1st Qu.: 1.60   1st Qu.:0.3  
##  Median : 5.750   Median : 3.000   Median : 4.50   Median :1.3  
##  Mean   : 6.559   Mean   : 3.391   Mean   : 4.45   Mean   :Inf  
##  3rd Qu.: 6.400   3rd Qu.: 3.300   3rd Qu.: 5.10   3rd Qu.:1.8  
##  Max.   :73.000   Max.   :30.000   Max.   :63.00   Max.   :Inf  
##  NA's   :10       NA's   :17       NA's   :19      NA's   :12   
##    Species         
##  Length:150        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 
# Count special values in each numeric column
num_cols <- sapply(dirty_iris, is.numeric)
sapply(dirty_iris[, num_cols], function(x) sum(is.infinite(x)))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
##            0            0            0            1

Answer: C – Inf. Petal.Width contains one Inf value.

Question 6 – Replace the special value with NA

inf_indicator <- is.infinite(dirty_iris$Petal.Width)
which(inf_indicator)   # row with the Inf value
## [1] 86
dirty_iris$Petal.Width[inf_indicator] <- NA

summary(dirty_iris)
##   Sepal.Length     Sepal.Width      Petal.Length    Petal.Width   
##  Min.   : 0.000   Min.   :-3.000   Min.   : 0.00   Min.   :0.100  
##  1st Qu.: 5.100   1st Qu.: 2.800   1st Qu.: 1.60   1st Qu.:0.300  
##  Median : 5.750   Median : 3.000   Median : 4.50   Median :1.300  
##  Mean   : 6.559   Mean   : 3.391   Mean   : 4.45   Mean   :1.207  
##  3rd Qu.: 6.400   3rd Qu.: 3.300   3rd Qu.: 5.10   3rd Qu.:1.800  
##  Max.   :73.000   Max.   :30.000   Max.   :63.00   Max.   :2.500  
##  NA's   :10       NA's   :17       NA's   :19      NA's   :13     
##    Species         
##  Length:150        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 

Answer: True.

Question 7 – Rule violations

Rules: Sepal.Width must be positive (> 0), and Sepal.Length cannot exceed 30 cm.

rules_violate <- subset(dirty_iris,
                        (Sepal.Width <= 0) | (Sepal.Length > 30))
rules_violate
##     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(rules_violate)
## [1] 4

Answer: E – 4 observations.

Question 8 – Correct the Sepal.Width errors

# Negative values -> absolute value
neg_indicator <- which(dirty_iris$Sepal.Width < 0)
dirty_iris$Sepal.Width[neg_indicator] <-
  abs(dirty_iris$Sepal.Width[neg_indicator])
dirty_iris[neg_indicator, ]
##    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 16            5           3          3.5           1 versicolor
# Zero values -> NA (imputed later)
zero_indicator <- which(dirty_iris$Sepal.Width == 0)
dirty_iris$Sepal.Width[zero_indicator] <- NA
dirty_iris[zero_indicator, ]
##     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 130          5.7          NA          1.7         0.3  setosa
# Confirm no non-positive widths remain
sum(dirty_iris$Sepal.Width <= 0, na.rm = TRUE)
## [1] 0

Answer: True.

Question 9 – Impute the missing values

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

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

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

# Petal.Width: kNN
dirty_iris <- kNN(dirty_iris, variable = "Petal.Width", imp_var = FALSE)

# Check: no missing values should remain
colSums(is.na(dirty_iris))
## Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
##            0            0            0            0            0
summary(dirty_iris)
##   Sepal.Length     Sepal.Width      Petal.Length     Petal.Width   
##  Min.   : 0.000   Min.   : 2.200   Min.   : 0.000   Min.   :0.100  
##  1st Qu.: 5.100   1st Qu.: 2.825   1st Qu.: 1.700   1st Qu.:0.300  
##  Median : 5.800   Median : 3.100   Median : 4.500   Median :1.300  
##  Mean   : 6.542   Mean   : 3.462   Mean   : 4.456   Mean   :1.208  
##  3rd Qu.: 6.400   3rd Qu.: 3.462   3rd Qu.: 5.100   3rd Qu.:1.800  
##  Max.   :73.000   Max.   :30.000   Max.   :63.000   Max.   :2.500  
##    Species         
##  Length:150        
##  Class :character  
##  Mode  :character  
##                    
##                    
## 

Answer: A, B, C, and D. All four imputations are complete, and no missing values remain.