forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot2.R
29 lines (16 loc) · 1.1 KB
/
plot2.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
## PREPARATION
## 1. Download the dataset from https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip
## 2. Unzip the file and place it in the working directory
## 3. Load the data (to save time and memory - only the first 70000 rows, as we need only data from the dates 2007-02-01 and 2007-02-02)
## 3.1. Set "?" as NA and define column classes
data1 <- read.csv("household_power_consumption.txt", na.strings = "?", sep = ";", nrows=70000,
colClasses = c("character", "character", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric"))
## 4. Convert the Date and Time variables to Date/Time classes
data1[,2]<-as.POSIXct(paste(data1[,1], data1[,2]), format="%d/%m/%Y %H:%M:%S")
data1[,1]<-as.Date(data1[,1], format = "%d/%m/%Y")
## 5. Extract only the data from the dates 2007-02-01 and 2007-02-02
data<-data1[data1$Date == "2007-02-01" | data1$Date == "2007-02-02", ]
## 6. Create the graph and save it into a png file
png(file = "plot2.png")
plot(data$Time, data$Global_active_power, type="l", xlab = "", ylab = "Global Active Power (kilowatts)")
dev.off()