Q.3 Reading data
dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
sum(is.na(dirty_iris$Petal.Length))
## [1] 19
Q.4 Calculating percentages of complete observations
rows_complete <- sum(complete.cases(dirty_iris))
mean(complete.cases(dirty_iris)) * 100
## [1] 64
Q.5 Deciphering hidden values
any(is.infinite(dirty_iris$Petal.Width))
## [1] TRUE
any(is.infinite(dirty_iris$Petal.Length))
## [1] FALSE
any(is.infinite(dirty_iris$Species))
## [1] FALSE
any(is.nan(dirty_iris$Sepal.Width))
## [1] FALSE
any(is.nan(dirty_iris$Petal.Length))
## [1] FALSE
any(is.nan(dirty_iris$Petal.Width))
## [1] FALSE
Q.6 Replacing specific values
inf_rows <- which(is.infinite(dirty_iris$Petal.Width))
print(inf_rows)
## [1] 86
print(dirty_iris$Petal.Width[inf_rows])
## [1] Inf
dirty_iris$Petal.Width[is.infinite(dirty_iris$Petal.Width)] <- NA
print(any(is.infinite(dirty_iris$Petal.Width)))
## [1] FALSE
summary(dirty_iris$Petal.Width)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.100 0.300 1.300 1.207 1.800 2.500 13
Q.7 correcting logical observation errors
bad_width <- dirty_iris$Sepal.Width <= 0
bad_length <- dirty_iris$Sepal.Length > 30
violating_rows <- bad_width | bad_length
print(which(violating_rows))
## [1] 16 28 125 130
violating_rows <- subset(dirty_iris, Sepal.Width <= 0 | Sepal.Length > 30)
print(violating_rows)
## 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
print(nrow(violating_rows))
## [1] 4
Q.8 Locating and correcting values
neg_rows <- which(dirty_iris$Sepal.Width < 0)
dirty_iris$Sepal.Width[neg_rows] <- abs(dirty_iris$Sepal.Width[neg_rows])
zero_rows <- which(dirty_iris$Sepal.Width == 0)
dirty_iris$Sepal.Width[zero_rows] <- NA
print(any(dirty_iris$Sepal.Width < 0, na.rm = TRUE))
## [1] FALSE
print(dirty_iris$Sepal.Width[c(35, 127)])
## [1] 2.9 3.2
Q.9 Imputing values into dataset
mean_sw <- mean(dirty_iris$Sepal.Width, na.rm = TRUE)
dirty_iris$Sepal.Width[is.na(dirty_iris$Sepal.Width)] <- mean_sw
median_pl <- median(dirty_iris$Petal.Length, na.rm = TRUE)
dirty_iris$Petal.Length[is.na(dirty_iris$Petal.Length)] <- median_pl
model_sl <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = dirty_iris)
pred_sl <- predict(model_sl, newdata = dirty_iris)
dirty_iris$Sepal.Length[is.na(dirty_iris$Sepal.Length)] <- pred_sl[is.na(dirty_iris$Sepal.Length)]