forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.R
38 lines (32 loc) · 1.18 KB
/
util.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
# Download the zip file, and extract it into the work folder,
# select the rows we need, add the date time as a new column,
# and then return the data.
getData <- function() {
# URL and files names
fileUrl <- 'https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip'
zipFile <- 'exdata-data-household_power_consumption.zip'
destFile <- 'household_power_consumption.txt'
# Download and unzip
if(!file.exists(zipFile)) {
download.file(fileUrl, zipFile, mode="wb")
}
if(!file.exists(destFile)) {
unzip(zipFile, destFile)
}
# Define column data types
cols <- c("character", "character", rep("numeric", 7))
# Using sqldf to load only data we need
if(!require('sqldf')){
install.packages('sqldf')
}
library(sqldf)
dataFrame <- read.csv(destFile, sep=";")
sql <- "select * from dataFrame where Date='2/2/2007' or Date='1/2/2007'"
data <- sqldf(sql)
# Get datetime for the plot and add them to the data
dateTime <- paste(data$Date, data$Time, sep=' ')
dateTime <- strptime(dateTime, format = "%d/%m/%Y %H:%M:%S")
data$DateTime <- dateTime
# Return the data
data
}