forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot1.R
32 lines (28 loc) · 1.09 KB
/
plot1.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
##find skip
skip_find<-function(inputFile,pattern,header=T){
con <- file(inputFile, open = "r")
if(header==T)
readLines(con, n = 1, warn = FALSE)#header
sk<-0
while (length(oneLine <- readLines(con, n = 1, warn = FALSE)) > 0) {
sk<-sk+1
if(length(grep(pattern,oneLine))==1)
break
}
close(con)
sk
}
##read data
df<-read.table(file="household_power_consumption.txt",header = F,sep=";",
na.strings = "?",stringsAsFactors = F,
skip=skip_find("household_power_consumption.txt","^1/2/2007"),
nrows = 2*24*60)
colnames(df)<-colnames(read.table(file="household_power_consumption.txt",header = T,sep=";",na.strings = "?",stringsAsFactors = F,nrows = 1))
df$Date<-as.Date(df$Date,"%d/%m/%Y")
df$Time<-strptime(paste(df$Date,df$Time),"%Y-%m-%d %H:%M:%S")
hist(df$Global_active_power,main="Global Active Power",col = "red",xlab = "Global Active Power (kilowatts)")
#dev.copy(png,"plot1.png")
#dev.off()
png(file="plot1.png")
hist(df$Global_active_power,main="Global Active Power",col = "red",xlab = "Global Active Power (kilowatts)")
dev.off()