forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
72 lines (65 loc) · 3.28 KB
/
plot1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Exploratory Data Analysis
# Course Project 1 (9/13/2015)
#
# Part 1 of 4
#######################################################################################
#
# Full project description available at:
# https://class.coursera.org/exdata-032/human_grading/view/courses/975127/assessments/3/submissions
#
# This project replicates several graphics based on Electric Power Consumption data
# from the UC Irvine Machine Learning Repository.
#
# The following code provides for downloading and extracting the raw data, loading the
# appropriate data for analysis into R and replicating Plot 1 of this project. The download
# and extract portion includes checks to prevent repeated handling of the large dataset in
# the case that is is already downloaded and/or extracted on the local system.
#
#######################################################################################
## FILE RETRIEVAL AND DATA EXTRACTION
zipfile <- "household_power_consumption.zip"
sourceurl <- paste0("https://d396qusza40orc.cloudfront.net/exdata/data/", zipfile)
datafile <- "household_power_consumption.txt"
# Setup data directory if it is not present
if(!file.exists("data")){dir.create( paste0(getwd(), "/data"))}
try(setwd("./data"), stop("Error: Unable to set working directory."))
# If the datafile is present don't repeat the download and extraction.
if(!file.exists(datafile)){
# Don't repeat the download if the zip file is found.
if(!file.exists(zipfile)){
try(
download.file(sourceurl, destfile = paste0("./",zipfile), method = "curl"),
stop("Error Unable to download remote zipfile.")
)
dateDownloaded <- date()
}
# Extract the data from new or existing zipfile
try(
unzip(zipfile, files = datafile, list = FALSE, overwrite = TRUE,
junkpaths = FALSE, exdir = ".", unzip = "internal",
setTimes = FALSE),
stop("Error: Unable to extract datafile from archive.")
)
}
## LOAD DATA
electric <- read.table(datafile, colClasses = c(date="character",
time="character",
global_active_power = "numeric",
global_reactive_power = "numeric",
voltage = "numeric",
global_intensity = "numeric",
sub_metering_1 = "numeric",
sub_metering_2 = "numeric",
sub_metering_3 = "numeric"
),
header=TRUE, sep=";", na.strings=c("?"), stringsAsFactors = FALSE
)
setwd("..") # done with data directory
## RETAIN DATA FOR ANALYSIS
selectedDates <- electric[ electric[1]=="1/2/2007" | electric[1]=="2/2/2007",]
rm(electric)
#GENERATE PLOT
# Plot 1 - Global Active Power Histogram
png("plot1.png", width = 480, height = 480, units = "px")
with(selectedDates, hist(Global_active_power, col="red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)", ylab = "Frequency"))
dev.off()