diff --git a/Data_Download.md b/Data_Download.md new file mode 100644 index 00000000000..9e7bc41c853 --- /dev/null +++ b/Data_Download.md @@ -0,0 +1,55 @@ +``` r +knitr::opts_chunk$set(echo = TRUE) +``` + +The dataset +----------- + +The *Individual household electric power consumption Data Set* is collected from the UC Irvine Machine Learning Repository and made available through the course web site () + +------------------------------------------------------------------------ + +Creating a directory for the dataset +------------------------------------ + +``` r +# make the directory named "data" if it does not exist +if(!file.exists("data")){ + dir.create("data") +} +``` + +------------------------------------------------------------------------ + +------------------------------------------------------------------------ + +Dataset download +---------------- + +``` r +fileUrl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip" +download.file(fileUrl, + destfile="./data/household_power_consumption.zip", + method="curl") +list.files("./data") +``` + + ## [1] "household_power_consumption.zip" + +``` r +unzip ("./data/household_power_consumption.zip", + exdir = "./data") +list.files("./data") +``` + + ## [1] "household_power_consumption.txt" "household_power_consumption.zip" + +``` r +#dataset download details +dateDownloaded <- date() +dateDownloaded +``` + + ## [1] "Sun Jun 23 18:39:49 2019" + +------------------------------------------------------------------------ diff --git a/data/dat.for.2days.RData b/data/dat.for.2days.RData new file mode 100644 index 00000000000..8675bf2745a Binary files /dev/null and b/data/dat.for.2days.RData differ diff --git a/data/household_power_consumption.zip b/data/household_power_consumption.zip new file mode 100644 index 00000000000..ae8864521e6 Binary files /dev/null and b/data/household_power_consumption.zip differ diff --git a/format.And.Filter.R b/format.And.Filter.R new file mode 100644 index 00000000000..e078b251d4b --- /dev/null +++ b/format.And.Filter.R @@ -0,0 +1,35 @@ + +power.data.subset <- read.table( + "./data/household_power_consumption.txt", + header=T, + na.strings = "?", + stringsAsFactors=F, + sep=";", + nrow=100) +household_power_consumption_all <- read.table( + "./data/household_power_consumption.txt", + na.strings = "?", + header=T, + stringsAsFactors=F, + sep=";", + colClasses = sapply(power.data.subset, class)) + +household_power_consumption_all$Date.Formatted<- as.Date( + household_power_consumption_all$Date, + format="%d/%m/%Y") + +datetime <- paste( + household_power_consumption_all$Date.Formatted, + household_power_consumption_all$Time) + +household_power_consumption_all$datetime <- strptime( + datetime, + "%Y-%m-%d %H:%M:%S") + +dat.for.2days <- subset( + household_power_consumption_all, + Date.Formatted == "2007-02-02" | Date.Formatted == "2007-02-01") + +save(dat.for.2days, + file = "./data/dat.for.2days.RData") + diff --git a/plot1.R b/plot1.R new file mode 100644 index 00000000000..8dc16c959ee --- /dev/null +++ b/plot1.R @@ -0,0 +1,22 @@ +#################################################################################################### +# +#+ Dataset download details are provided in the "Data_Download.md" +#+ Downloaded dataset is available in the folder "data" +#+ The downloaded dataset has been read, formatted, filtered and saved using "format.And.Filter.R" +#+ The filtered dataset is saved to the directory "data" ("dat.for.2days.RData") +# +#################################################################################################### + +#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) +load(file = "./data/dat.for.2days.RData") +ls() + +#+ plot-1 +png(filename = "plot1.png", + width = 480, height = 480, units = "px") +hist(dat.for.2days$Global_active_power, + main="Global Active Power", + xlab="Global Active Power (kilowatts)", + col="red" ) +dev.off() + diff --git a/plot1.png b/plot1.png new file mode 100644 index 00000000000..db485ff3905 Binary files /dev/null and b/plot1.png differ diff --git a/plot2.R b/plot2.R new file mode 100644 index 00000000000..2f01b13c76a --- /dev/null +++ b/plot2.R @@ -0,0 +1,23 @@ +#################################################################################################### +# +#+ Dataset download details are provided in the "Data_Download.md" +#+ Downloaded dataset is available in the folder "data" +#+ The downloaded dataset has been read, formatted, filtered and saved using "format.And.Filter.R" +#+ The filtered dataset is saved to the directory "data" ("dat.for.2days.RData") +# +#################################################################################################### + + +#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) +load(file = "./data/dat.for.2days.RData") +ls() + +#+ plot-2 +png(filename = "plot2.png", + width = 480, height = 480, units = "px") +with (dat.for.2days, + plot(datetime, Global_active_power, type="l", xlab="", + ylab="Global Active Power (kilowatts)", main="") +) +dev.off() + diff --git a/plot2.png b/plot2.png new file mode 100644 index 00000000000..08381f8befb Binary files /dev/null and b/plot2.png differ diff --git a/plot3.R b/plot3.R new file mode 100644 index 00000000000..2f01472004a --- /dev/null +++ b/plot3.R @@ -0,0 +1,50 @@ +#################################################################################################### +# +#+ Dataset download details are provided in the Data_Download.md +#+ Downloaded dataset is available in the folder "data" +#+ The downloaded dataset has been read, formatted, filtered and saved using format.And.Filter.R +#+ The filtered dataset is saved to the directory "data" and is named "dat.for.2days.RData" +# +#################################################################################################### + +#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) +load(file = "./data/dat.for.2days.RData") +ls() + +#+ plot-3 +png( + filename = "plot3.png", + width = 480, height = 480, units = "px" +) + +with ( + dat.for.2days, + plot(datetime, Sub_metering_1, type="l", col="black", xlab="", + ylab="Energy sub metering", main="") +) + +lines ( + dat.for.2days$datetime, + dat.for.2days$Sub_metering_2, + col="red", xlab="", + ylab="", main="" +) + +lines ( + dat.for.2days$datetime, + dat.for.2days$Sub_metering_3, + col="blue", xlab="", + ylab="", main="" +) + +# Add legend +legend( + "topright", + legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), + col=c("black", "red", "blue"), lty=1 +) + + +dev.off() + + diff --git a/plot3.png b/plot3.png new file mode 100644 index 00000000000..38ac5a873ae Binary files /dev/null and b/plot3.png differ diff --git a/plot4.R b/plot4.R new file mode 100644 index 00000000000..3e9d9a61612 --- /dev/null +++ b/plot4.R @@ -0,0 +1,92 @@ +#################################################################################################### +# +#+ Dataset download details are provided in the Data_Download.md +#+ Downloaded dataset is available in the folder "data" +#+ The downloaded dataset has been read, formatted, filtered and saved using format.And.Filter.R +#+ The filtered dataset is saved to the directory "data" and is named "dat.for.2days.RData" +# +#################################################################################################### + + +#loading 2-day household energy usage data (2007-02-01 and 2007-02-02) +load(file = "./data/dat.for.2days.RData") +ls() + +#+ plot-4 +png(filename = "plot4.png", + width = 480, height = 480, units = "px") + +par(mfrow = c(2, 2)) + +################## +# +#+plot-4.1 +# +################## +with (dat.for.2days, + plot(datetime, Global_active_power, type="l", xlab="", + ylab="Global Active Power", main="" + ) +) + +################## +# +#plot-4.2 +# +################## +with (dat.for.2days, + plot( + datetime, Voltage, type="l", + ylab="Voltage", + main="" + ) +) + +################## +# +#plot-4.3 +# +################## +with ( + dat.for.2days, + plot(datetime, Sub_metering_1, type="l", col="black", xlab="", + ylab="Energy sub metering", main="") +) + +lines ( + dat.for.2days$datetime, + dat.for.2days$Sub_metering_2, + col="red", xlab="", + ylab="", main="" +) + +lines ( + dat.for.2days$datetime, + dat.for.2days$Sub_metering_3, + col="blue", xlab="", + ylab="", main="" +) + +# Add legend +legend( + "topright", + legend=c("Sub_metering_1", "Sub_metering_2", "Sub_metering_3"), + col=c("black", "red", "blue"), lty=1 +) + + + +################## +# +#plot-4.4 +# +################## +with (dat.for.2days, + plot( + datetime, Global_reactive_power, type="l", + main="" + ) +) + + +dev.off() \ No newline at end of file diff --git a/plot4.png b/plot4.png new file mode 100644 index 00000000000..8ff2e5683c8 Binary files /dev/null and b/plot4.png differ