forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot1.R
33 lines (28 loc) · 1.37 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
if (!file.exists("household_power_consumption_subset.saved.R")){
print("loading data from web and processing, might take a while")
# downloading file
zipurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
tempzip <- tempfile()
download.file(zipurl,tempzip)
unzip(tempzip)
unlink(tempzip)
# read the entire table and process it
data <- read.table("household_power_consumption.txt", colClasses=c("character","character",rep("numeric",7)),
header=T, sep=";", nrows=-1, strip.white=T, na.strings="?")
data$time <- dmy_hms(paste(data$Date, data$Time))
data$Date <- NULL
data$Time <- NULL
# subset and save data from first two day of Feb 2007
subsetdata <- data[data$time<dmy("03/02/2007") & data$time>dmy("01/02/2007"),]
save(subsetdata, file="household_power_consumption_subset.saved.R")
# clean the environment and apply garbage collector
rm(data)
gc()
} else {
print("loading saved data")
load("household_power_consumption_subset.saved.R")
}
# create 'Global Ative Power' hist in png file
png(filename="plot1.png")
hist(subsetdata$Global_active_power, col="red", xlab="Global Active Power (killowats)", main="Global Active Power")
dev.off()