This document provides an analysis of the airquality
dataset. We will perform data cleaning, handle missing values, and
create various visualizations to explore the relationships in the
data.
# Load the dataset
data(airquality)
# Display the first few rows of the dataset
head(airquality)
## Ozone Solar.R Wind Temp Month Day
## 1 41 190 7.4 67 5 1
## 2 36 118 8.0 72 5 2
## 3 12 149 12.6 74 5 3
## 4 18 313 11.5 62 5 4
## 5 NA NA 14.3 56 5 5
## 6 28 NA 14.9 66 5 6
# Structure of the dataset
str(airquality)
## 'data.frame': 153 obs. of 6 variables:
## $ Ozone : int 41 36 12 18 NA 28 23 19 8 NA ...
## $ Solar.R: int 190 118 149 313 NA NA 299 99 19 194 ...
## $ Wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ Temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ Month : int 5 5 5 5 5 5 5 5 5 5 ...
## $ Day : int 1 2 3 4 5 6 7 8 9 10 ...
# Check for missing values
colSums(is.na(airquality))
## Ozone Solar.R Wind Temp Month Day
## 37 7 0 0 0 0
Data Cleaning
# Install and load the dplyr package
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.4'
## (as 'lib' is unspecified)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Impute missing values
airquality$Ozone <- ifelse(is.na(airquality$Ozone), median(airquality$Ozone, na.rm = TRUE), airquality$Ozone)
airquality$Solar.R <- ifelse(is.na(airquality$Solar.R), median(airquality$Solar.R, na.rm = TRUE), airquality$Solar.R)
# Confirm that there are no missing values left
colSums(is.na(airquality))
## Ozone Solar.R Wind Temp Month Day
## 0 0 0 0 0 0
# Convert Month and Day to factors
airquality$Month <- as.factor(airquality$Month)
airquality$Day <- as.factor(airquality$Day)
# Convert column names to lowercase and replace dots with dashes
names(airquality) <- tolower(names(airquality))
names(airquality) <- gsub("\\.", "-", names(airquality))
# Create a new variable for the ozone-to-solar ratio
airquality <- airquality %>% mutate(ozone_solar_ratio = ozone / `solar-r`)
# Remove duplicate rows
airquality <- airquality[!duplicated(airquality), ]
# Display the structure of the cleaned dataset
str(airquality)
## 'data.frame': 153 obs. of 7 variables:
## $ ozone : num 41 36 12 18 31.5 28 23 19 8 31.5 ...
## $ solar-r : num 190 118 149 313 205 205 299 99 19 194 ...
## $ wind : num 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
## $ temp : int 67 72 74 62 56 66 65 59 61 69 ...
## $ month : Factor w/ 5 levels "5","6","7","8",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ day : Factor w/ 31 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ...
## $ ozone_solar_ratio: num 0.2158 0.3051 0.0805 0.0575 0.1537 ...
Time Series Plot of Ozone Levels
library(ggplot2)
ggplot(airquality, aes(x = 1:nrow(airquality), y = ozone)) +
geom_line(color = "navy") +
xlab("Day") +
ylab("Ozone") +
ggtitle("Time Series Plot of Ozone Levels")
Boxplot of Ozone by Month
ggplot(airquality, aes(x = as.factor(month), y = ozone)) +
geom_boxplot() +
xlab("Month") +
ylab("Ozone") +
ggtitle("Boxplot of Ozone by Month")
Histogram of Temperature
ggplot(airquality, aes(x = temp)) +
geom_histogram(binwidth = 1, fill = "violet", color = "white") +
xlab("Temperature") +
ylab("Count") +
ggtitle("Histogram of Temperature")
Scatter Plot of Ozone vs. Wind
ggplot(airquality, aes(x = wind, y = ozone)) +
geom_point(color = "darkgreen") +
xlab("Wind") +
ylab("Ozone") +
ggtitle("Scatter Plot of Ozone vs. Wind")
Ozone vs Wind by Month
ggplot(airquality, aes(x = wind, y = ozone)) +
geom_point() +
facet_wrap(~ month) +
xlab("Wind") +
ylab("Ozone") +
ggtitle("Ozone vs Wind by Month")
In this analysis, we cleaned the airquality dataset by handling missing values, transforming variables, and removing duplicates. We then created various visualizations to explore the dataset, including time series plots, boxplots, histograms, scatter plots, and faceted plots. These visualizations help in understanding the patterns and relationships within the data.