Part I

The goal of this part is to examine how household energy usage varies over a 2-day period in February, 2007 using the base plotting system. The data used in here (in particular, the “Individual household electric power consumption Data Set”) are from the UC Irvine Machine Learning Repository, a popular repository for machine learning datasets.

library(sqldf)
library(dplyr)
library(ggplot2)
# Estimate the memory usage
paste0("As the dataset has 2880 rows and 9 columns, it requires about ", round(2880 * 9 * 8 / 2^20, 2), " Megabytes in memory.")
## [1] "As the dataset has 2880 rows and 9 columns, it requires about 0.2 Megabytes in memory."
# Read the data using an SQL statement
sel_data <- read.csv.sql("household_power_consumption.txt", sep = ';', header = TRUE,sql="select * from file where Date in ('1/2/2007', '2/2/2007')")
paste0("Used memory: ", format(object.size(sel_data), units = "Mb"))
## [1] "Used memory: 0.3 Mb"
# Create a datetime column given a specific format
sel_data <- sel_data %>% mutate(datetime = as.POSIXct(paste(sel_data$Date, sel_data$Time), format="%d/%m/%Y %H:%M:%S"))

# Plot the histogram
hist(sel_data$Global_active_power, col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")

# Create a png file from the histogram
png(file = "plot1.png", width = 480, height = 480, bg = "transparent")
hist(sel_data$Global_active_power, col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)")
dev.off()
plot(sel_data$datetime, sel_data$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power (kilowatts)")

par(mfrow = c(2, 2))
plot(sel_data$datetime, sel_data$Global_active_power, type = "l", xlab = "", ylab = "Global Active Power")
plot(sel_data$datetime, sel_data$Voltage, type = "l", xlab = "datetime", ylab = "Voltage")
plot(sel_data$datetime, sel_data$Sub_metering_1, type = "l", ylim = range(sel_data$Sub_metering_1), xlab = "", ylab= "Energy sub metering")
par(new = TRUE)
plot(sel_data$datetime, sel_data$Sub_metering_2, type = "l", ylim = range(sel_data$Sub_metering_1), col = "red", xaxt = "n", yaxt = "n", xlab = "", ylab= "")
par(new = TRUE)
plot(sel_data$datetime, sel_data$Sub_metering_3, type = "l", ylim = range(sel_data$Sub_metering_1), col = "blue", xaxt = "n", yaxt = "n", xlab = "", ylab= "")
legend("topright",legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), bty = "n", lty = c(1,1),col=c("black", "red", "blue"))
plot(sel_data$datetime, sel_data$Global_reactive_power, type = "l", xlab = "datetime", ylab = "Global_reactive_power")

Part II

Fine particulate matter (PM2.5) is an ambient air pollutant for which there is strong evidence that it is harmful to human health. In the United States, the Environmental Protection Agency (EPA) is tasked with setting national ambient air quality standards for fine PM and for tracking the emissions of this pollutant into the atmosphere. Approximatly every 3 years, the EPA releases its database on emissions of PM2.5. This database is known as the National Emissions Inventory (NEI). More information about the NEI are available at the EPA National Emissions Inventory web site.

For each year and for each type of PM source, the NEI records how many tons of PM2.5 were emitted from that source over the course of the entire year. The data used in this project are for 1999, 2002, 2005, and 2008 and can be downloaded here.

In this part of the project, I explore the National Emissions Inventory database and see what it says about fine particulate matter pollution in the United states over the 10-year period 1999–2008.

# Data frame with all of the PM2.5 emissions data for 1999, 2002, 2005, and 2008
NEI <- readRDS("summarySCC_PM25.rds")

# mapping from the SCC digit strings in the Emissions table to the actual name of the PM2.5 source
SCC <- readRDS("Source_Classification_Code.rds")

Have total emissions from PM2.5 decreased in the United States from 1999 to 2008?

totalEm <- with(NEI, tapply(Emissions, year, sum, na.rm = TRUE))
plot(names(totalEm), totalEm, type ="l", xlab = "Year", ylab = "Total Pm(2.5) Emissions (tons)", main = "Evolution of the total PM2.5 Emissions over the US")

Have total emissions from PM2.5 decreased in the Baltimore City, Maryland (fips==“24510”) from 1999 to 2008?

NEIBal <- subset(NEI, fips == "24510")
totalEmBal <- with(NEIBal, tapply(Emissions, year, sum, na.rm = TRUE))
plot(names(totalEmBal), totalEmBal, type ="l", xlab = "Year", ylab = "Total Pm2.5 Emissions (tons)", main = "Evolution of the total PM2.5 Emissions in Baltimore")

Of the four types of sources indicated by the type (point, nonpoint, onroad, nonroad) variable, which of these four sources have seen decreases in emissions from 1999–2008 for Baltimore City? Which have seen increases in emissions from 1999–2008?

totalEmBalTypes <- aggregate(NEIBal$Emissions, by=list(type=NEIBal$type , year=NEIBal$year), FUN = sum)
with(totalEmBalTypes, qplot(year, x, color = type, geom= c("point", "line"), xlab = "Year", ylab = "Total PM2.5 Emissions (tons)", main = "Evolution of total Emissions in Baltimore by type of source"))

How have emissions from motor vehicle sources changed from 1999–2008 in Baltimore City?

NEIBal <- subset(NEI, fips == "24510" & type == "ON-ROAD")
totalBalMotor <- with(NEIBal, tapply(Emissions, year, sum, na.rm = TRUE))
plot(names(totalBalMotor), totalBalMotor, type ="l", xlab = "Year", ylab = "Total Pm2.5 Emissions (tons)", main = "Total Emission from Motor Vehicles in Baltimore City")

Compare emissions from motor vehicle sources in Baltimore City with emissions from motor vehicle sources in Los Angeles County, California (fips==“06037”). Which city has seen greater changes over time in motor vehicle emissions?

NEIComp <- subset(NEI, fips %in% c("24510", "06037") & type == "ON-ROAD")
totalComp <- aggregate(NEIComp$Emissions, by=list(fips= NEIComp$fips, type=NEIComp$type, year=NEIComp$year), FUN = sum)
with(totalComp, qplot(year, x, color = fips, geom= c("point", "line"), xlab = "Year", ylab = "Total PM2.5 Emissions (tons)", main = "Evolution of total Emissions from Motor Vehicles in Baltimore city \n      (fips = 24510) and Los Angeles County (fips = 06037)"))