Question 3
d <- read.csv("https://raw.githubusercontent.com/edwindj/datacleaning/master/data/dirty_iris.csv")
sum(is.na(d$Petal.Length))
## [1] 19
Question 4
sum(complete.cases(d))
## [1] 96
mean(complete.cases(d)) * 100
## [1] 64
Question 5
sapply(d[sapply(d, is.numeric)], function(x) c(
sum(is.nan(x)),
sum(is.infinite(x) & x > 0, na.rm = TRUE),
sum(is.infinite(x) & x < 0, na.rm = TRUE)
))
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## [1,] 0 0 0 0
## [2,] 0 0 0 1
## [3,] 0 0 0 0
Question 6
which(is.infinite(as.matrix(d)), arr.ind = TRUE)
## row col
which(is.nan(as.matrix(d)), arr.ind = TRUE)
## row col
d[sapply(d, is.numeric)] <- lapply(d[sapply(d, is.numeric)], function(x) {x[is.infinite(x) | is.nan(x)] <- NA; x})
Question 7
bad <- which(d$Sepal.Width <= 0 | d$Sepal.Length > 30)
d[bad, ]
## 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
length(bad)
## [1] 4
Question 8
d[d$Sepal.Width < 0 & !is.na(d$Sepal.Width), "Sepal.Width"] <- abs(d$Sepal.Width[d$Sepal.Width < 0 & !is.na(d$Sepal.Width)])
d$Sepal.Width[d$Sepal.Width == 0] <- NA
d[bad, ]
## 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 NA 1.7 0.3 setosa
Question 9
d$Sepal.Length[d$Sepal.Length > 30] <- NA
d$Sepal.Width[is.na(d$Sepal.Width)] <- mean(d$Sepal.Width, na.rm = TRUE)
d$Petal.Length[is.na(d$Petal.Length)] <- median(d$Petal.Length, na.rm = TRUE)
library(VIM)
## Warning: package 'VIM' was built under R version 4.4.3
## Loading required package: colorspace
## 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
d <- kNN(d, variable = "Petal.Width", k = 5, imp_var = FALSE)
m <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data = d)
i <- is.na(d$Sepal.Length)
d$Sepal.Length[i] <- predict(m, newdata = d[i, ])
colSums(is.na(d))
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 0 0 0 0 0