dirty_iris <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
sum(is.na(dirty_iris$Petal.Length))
## [1] 19
##returns 19
sum(complete.cases(dirty_iris))
## [1] 96
##96
nrow(dirty_iris)
## [1] 150
##150,,math…
sum(complete.cases(dirty_iris)) / nrow(dirty_iris) * 100
## [1] 64
##64 ##question6
dirty_iris[] <- lapply(dirty_iris, function(x) {
if (is.numeric(x)) x[is.nan(x)] <- NA
x
})
dirty_iris[] <- lapply(dirty_iris, function(x) {
if (is.numeric(x)) {
x[is.nan(x)] <- NA
x[is.infinite(x)] <- NA
}
x
})
##returns 4
neg_idx <- !is.na(dirty_iris$Sepal.Width) & dirty_iris$Sepal.Width < 0
dirty_iris$Sepal.Width[neg_idx] <- abs(dirty_iris$Sepal.Width[neg_idx])
zero_idx <- !is.na(dirty_iris$Sepal.Width) & dirty_iris$Sepal.Width == 0
dirty_iris$Sepal.Width[zero_idx] <- NA
##mean,median,regression,kNN
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)
if (!requireNamespace("VIM", quietly = TRUE)) install.packages("VIM")
library(VIM)
## Warning: package 'VIM' was built under R version 4.5.2
## Loading required package: colorspace
## Warning: package 'colorspace' was built under R version 4.5.2
## 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
tmp <- kNN(dirty_iris, variable = "Petal.Width", k = 5)
dirty_iris$Petal.Width <- tmp$Petal.Width
train <- complete.cases(dirty_iris[, c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")])
model <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = dirty_iris[train, ])
missing <- is.na(dirty_iris$Sepal.Length)
dirty_iris$Sepal.Length[missing] <- predict(model, newdata = dirty_iris[missing, ])