forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpen_Data.R
26 lines (25 loc) · 1.04 KB
/
Open_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
setwd("C:/Users/My/Desktop/Data Science Course/Exploratory Data Analysis/Course Project Week 1")
## set the working directory###
if (!file.exists("./PowerConsumption.csv")) {
# Read the Data to a data frame
data <- read.table("./household_power_consumption.txt", header = TRUE, sep = ";", na = "?", colClasses = c(rep("character", 2), rep("numeric", 7)))
attach(data)
##filter data frame##
data <- data[(Date == "1/2/2007" | Date == "2/2/2007"), ]
attach(data)
##setting format##
data$DateTime <- strptime(paste(Date, Time), "%d/%m/%Y %H:%M:%S")
rownames(data) <- 1 : nrow(data)
attach(data)
data <- cbind(data[, 10], data[, 3:9])
colname <- colnames(data)
colname[1] <- "Date_time"
colnames(data) <- colname
attach(data)
##Create a csv file##
write.csv(data, file = "./PowerConsumption.csv", row.names = FALSE)
} else {
# Read the Data to a data frame
data <- read.csv(file = "./PowerConsumption.csv", header = TRUE)
attach(data)
}