Repository navigation
Expand file tree
/
Copy pathplot2.R
More file actions
64 lines (51 loc) · 2.29 KB
/
Copy pathplot2.R
File metadata and controls
64 lines (51 loc) · 2.29 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Exploratory Data Analysis
# Course Project, Week 1
# Coursera
# Sarah Gartenmann
# February 2021
################################################################################
# Setting working directory and creating a new directory for the files
setwd("C:/Users/gartens1/Downloads")
if (!file.exists("./coursera_exploratorydataanalysis_courseproject1")) {
dir.create("./coursera_exploratorydataanalysis_courseproject1")
}
# downloading the file
fileurl <- "https://d396qusza40orc.cloudfront.net/exdata%2Fdata%2Fhousehold_power_consumption.zip"
download.file(fileurl,
destfile = "./coursera_exploratorydataanalysis_courseproject1/dataset.zip")
# Unuipping the zip file
unzip(zipfile = "./coursera_exploratorydataanalysis_courseproject1/dataset.zip",
exdir = "./coursera_exploratorydataanalysis_courseproject1")
# reading in dataset
data <- read.table("./coursera_exploratorydataanalysis_courseproject1/household_power_consumption.txt",
header = TRUE, sep = ";")
# subsetting the data to only show data from 2007-02-01 and 2007-02-02
# ("1/2/2007" and "2/2/2007", respectively)
names(data)
datasub <- subset(data, Date == "1/2/2007" | Date == "2/2/2007")
names(datasub)
################################################################################
# Plot 2
# change "Global_active_power" from factor to numeric
# (2880obs, 9 variables)
# datasub[,3] <- sapply(datasub[,3], as.numeric)
# create a date-time column with strptime() from Date and Time columns in datasub
# (POSIXlt[1:2880])
datetime <- strptime(paste(datasub[,1], datasub[,2], sep=" "), "%d/%m/%Y %H:%M:%S")
# cbind "datetime" and "datsub" together to create a new dataframe
# (2880obs, 10 variables)
datasub2 <- as.data.frame(cbind(datetime, datasub))
# remove uncessary columns from datasub2
# (2880obs, 2 variables)
datasub2 <- datasub2[,-c(2,3,5:10)]
# creating and saving the plot as a PNG file
png(file = "./coursera_exploratorydataanalysis_courseproject1/plot2.png",
width = 480, height = 480)
# plot graph
with(datasub2,
plot(datetime, as.numeric(as.character(Global_active_power)),
type = "l",
xlab = "Day",
ylab = "Global Active Power (kilowatts)"))
# dev.off() to create the file
dev.off()