forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep_and_load_data.R
48 lines (38 loc) · 1.18 KB
/
prep_and_load_data.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
#
# exploratory data analysis - project 1
#
# script to download, prepare and load data for plots
#
# note - include lazy init mechanism so that plot scripts can call
# this script w/o worries about redoing multiple times costly ops
# (a.k.a. operations will be done only if not yet done prior in
# the same R session)
#
if (!exists("hpcDataToPlot")) {
print("downloading and preparting HPC data for plots...")
url <- "https://d396qusza40orc.cloudfront.net/exdata/data/household_power_consumption.zip"
localFile <- tempfile()
download.file(url, localFile, method = "curl", mode = "wb")
require(readr)
hpcData <- read_delim(
unz(localFile, "household_power_consumption.txt"),
delim = ";",
na = c("", "NA", "?"),
col_types = cols(
Date = col_character(),
Time = col_character(),
.default = "d"
),
locale = default_locale()
)
require(dplyr)
hpcDataToPlot <- filter(
hpcData,
Date == "1/2/2007" | Date == "2/2/2007"
) %>% mutate(
Datetime = as.POSIXct(strptime(paste(Date, Time, sep = " "), format = "%d/%m/%Y %H:%M:%S"))
)
file.remove(localFile)
} else {
print("re-using hpcDataToPlot dataset already in session")
}