dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
sum(is.na(dirty_iris$Petal.Length))
## [1] 19
num_complete_cases <- sum(complete.cases(dirty_iris))
percent_complete_cases <- (num_complete_cases / nrow(dirty_iris)) * 100
cat("Number of complete observations:", num_complete_cases, "\n")
## Number of complete observations: 96
cat("Percentage of complete observations:", percent_complete_cases, "%\n")
## Percentage of complete observations: 64 %
special_values <- sapply(dirty_iris, function(x) {
if (is.numeric(x)) {
list(
Inf_values = sum(is.infinite(x)),
NaN_values = sum(is.nan(x))
)
} else {
NULL
}
})
print(special_values)
## $Sepal.Length
## $Sepal.Length$Inf_values
## [1] 0
##
## $Sepal.Length$NaN_values
## [1] 0
##
##
## $Sepal.Width
## $Sepal.Width$Inf_values
## [1] 0
##
## $Sepal.Width$NaN_values
## [1] 0
##
##
## $Petal.Length
## $Petal.Length$Inf_values
## [1] 0
##
## $Petal.Length$NaN_values
## [1] 0
##
##
## $Petal.Width
## $Petal.Width$Inf_values
## [1] 1
##
## $Petal.Width$NaN_values
## [1] 0
##
##
## $Species
## NULL
violations <- dirty_iris[(dirty_iris$Sepal.Width <= 0) | (dirty_iris$Sepal.Length > 30), ]
nrow(violations)
## [1] 31
dirty_iris[] <- lapply(dirty_iris, function(x) {
if (is.numeric(x)) {
x[is.infinite(x)] <- NA # Replace Inf and -Inf with NA
x[is.nan(x)] <- NA # Replace NaN with NA
}
return(x)
})
sapply(dirty_iris, function(x) sum(is.na(x)))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 10 17 19 13 0
invalid_sepal_width <- dirty_iris$Sepal.Width <= 0
invalid_sepal_length <- dirty_iris$Sepal.Length > 30
violating_rows <- which(invalid_sepal_width | invalid_sepal_length)
num_violations <- length(violating_rows)
cat("Number of observations violating the rules:", num_violations, "\n")
## Number of observations violating the rules: 4
dirty_iris[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
NA
## [1] NA
dirty_iris$Sepal.Width[is.na(dirty_iris$Sepal.Width)] <- mean(dirty_iris$Sepal.Width, na.rm = TRUE)
dirty_iris$Petal.Length[is.na(dirty_iris$Petal.Length)] <- median(dirty_iris$Petal.Length, na.rm = TRUE)
model_sepal_length <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = dirty_iris)
dirty_iris$Sepal.Length[is.na(dirty_iris$Sepal.Length)] <- predict(model_sepal_length, newdata = dirty_iris[is.na(dirty_iris$Sepal.Length), ])