forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot1.R
More file actions
29 lines (18 loc) · 831 Bytes
/
plot1.R
File metadata and controls
29 lines (18 loc) · 831 Bytes
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
library(data.table)
library(sqldf)
## Set file path
sourceFile<-"./household_power_consumption.txt"
destFile<-"./plot1.png"
## use sql commands to read only part of teh file
mySql <- "SELECT * from file WHERE Date = '1/2/2007' OR Date = '2/2/2007'"
dataSet<-read.csv2.sql(sourceFile,mySql)
## convert data.frame to data.table
dataSet<-as.data.table(dataSet)
## convert Date and Time to single DateTime column of type POSIXct
dataSet[,DateTime:=as.POSIXct(paste0(dataSet$Date,dataSet$Time),format=format("%d/%m/%Y%H:%M:%S"))]
hist(dataSet$Global_active_power,main="Global Active Power",col="red",xlab="Global Active Power (kW)")
## copy from screen device to PNG file with required resolution
if(file.exists(destFile)) file.remove(destFile)
dev.copy(png, file=destFile,width=480,height=480)
## close png deivce
dev.off()