forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
50 lines (40 loc) · 1.45 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
#
# R code for Course Project 1 in the
# Coursera course "Exploratory Data Analysis"
#
# This file creates plot1.png according to the plot given in the assignment
# Section One: Get the data ----
#
# set the source of the data
dataFile <- "./data/household_power_consumption.txt"
# create a read-only connection to the text file
connection <- file(dataFile, open = "rt")
# loop control variable
x <- TRUE
# loop until we find the date of interest
while (x)
{
# read one line at a time until we find the last entry prior
# to 1/2/2007. Once this is found, x becomes FALSE and the
# loop exits. We need to stop at the entry before the desired
# date so that read.table will start at the correct date
x <- !grepl("31/1/2007;23:59:00", readLines(connection, n = 1))
}
# read from the connection (2 days X 24 hours X 60 minutes) of data
# according to the instructions, NAs are represented as ?
# the data is delimited by ;
data <- read.table(connection, header = F, na.strings = "?", sep = ";", nrows = 2880)
# cleanup
close(connection)
# Section Two: Massage the data ----
#
# read the first line of the file to get the column names for our subset
colnames(data) <- colnames(read.table(dataFile, header = T, nrows = 1, sep = ";"))
# Section Three: Plot the data ----
#
# open the PNG device
png("plot1.png")
# plot
with(data, hist(Global_active_power, col = "red", main = "Global Active Power", xlab = "Global Active Power (kilowatts)"))
# write
dev.off()