forked from rdpeng/ExData_Plotting1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 16.9 KB
/
.Rhistory
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
e <- rnorm(10, 0, 20)
e
?rnorm
system.time(readLines("http://www.jhsph.edu"))
library(datasets)
Rprof()
fit <- lm(y ~ x1 + x2)
Rprof(NULL)
y <-5
x1 <-6
x2<-7
library(datasets)
Rprof()
fit <- lm(y ~ x1 + x2)
Rprof(NULL)
summaryRprof()
?summaryrprof
?summaryRprof
x1 <-?~
?~
?ppois
set.seed(1)
rpois(5, 2)
?qpois
install.packages("swirl")
library('swirl')
swirl()
5+7
x <- 5+7
x
y <- x-3
y
z <- c(1.1, 9, 3.14)
?c
z
c(z,555,z)
z*2+100
my_sqrt <- sqrt(z-1)
my_sqrt
my_div <- z/my_sqrt
my_div
c(1, 2, 3, 4) + c(0, 10)
c(1, 2, 3, 4) + c(0, 10, 100)
z*2+1000
my_div
ls()
class(plants)
dim(plants)
nrow(plants)
ncol(plants)
object.size(plants)
names(plants)
head(plants)
head(plants,10)
tail(plants,15)
summary(plants)
table(plants$Active_Growth_Period)
str(plants)
?sample
sample(1:6,4,replace=TRUE)
sample(1:6,4,replace=TRUE)
sample(1:20,10,replace=FALSE)
sample(1:20,10)
LETTERS
sample(LETTERS)
flips <- sample(c(0,1), 100, replace=TRUE,prob=c(0.3,0.7))
flips
sum(flips)
?rbinom
rbinom(1, size = 100, prob = 0.7)
flips2 <- rbinom(100, size = 1, prob = 0.7)
flips2
sum(flips2)
?rnorm
rnorm(10)
rnorm(10, mean=100, sd = 25)
rpois(5, mean=10)
?rpois
rpois(5, 10)
my_pois <- replicate(100, rpois(5, 10))
my_pois
cm <- colMeans(my_pois)
hist(cm)
data(cars)
?cars
head(cars)
plot(cars)
?plot
plot(x=cars$speed, y = cars$dist)
plot(x=cars$dist, y = cars$speed)
plot(x=cars$speed, y = cars$dist)
plot(x=cars$speed, y = cars$dist, xlab = "Speed")
plot(x=cars$speed, y = cars$dist, xlab = "Speed", ylab = "Stopping Distance")
plot(x=cars$speed, y = cars$dist, ylab = "Stopping Distance")
plot(x=cars$speed, y = cars$dist, xlab = "Speed", ylab = "Stopping Distance")
plot(cars, main = "My Plot")
plot(cars, subtitle = "My Plot")
plot(cars, sub = "My Plot")
plot(cars, sub = "My Plot Subtitle")
plot(cars, col=2)
plot(cars, xlim = c(10,15))
plot(cars, pch = 2
)
data(mtcars)
?boxplot
boxplot(mpg ~ cyl, data = mtcars)
hist(mtcars$mpg)
sapply(flags, unique)
vapply(flags, unique, numeric(1))
ok()
sapply(flags, class)
vapply(flags, class, character(1))
?tapply
table(flags$landmass)
table(flags$animate)
tapply(flags$animate,flags$landmass,mean)
tapply(flags$population, flags$red, summary)
tapply(flags$population, flags$landmass, summary)
install.packages("xlsx")
install.packages("xlsx")
install.packages("xlsx")
install.packages("rJava")
R CMD javareconf
install.packages("rJava")
library(httr)
oauth_endpoints("github")
myapp <- oauth_app("f83f0a3eb4cd4809d85f", "d7ce20982cead1753b8f52bad2d2c22ba9362d50")
myapp <- oauth_app("github", f83f0a3eb4cd4809d85f", "d7ce20982cead1753b8f52bad2d2c22ba9362d50")
myapp <- oauth_app("github", f83f0a3eb4cd4809d85f")
)
myapp <- oauth_app("github", "f83f0a3eb4cd4809d85f", "d7ce20982cead1753b8f52bad2d2c22ba9362d50")
myapp
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp, cache=FALSE)
gtoken <- config(token = github_token)
req <- GET("https://api.github.com/users/jtleek/repos", gtoken)
head(req)
stop_for_status(req)
content(req)
library("httpuv")
install.packages("httpuv")
req
head(req)
req
library(httpuv)
oauth_endpoints("github")
myapp <- oauth_app("f83f0a3eb4cd4809d85f", "d7ce20982cead1753b8f52bad2d2c22ba9362d50")
myapp <- oauth_app("github","f83f0a3eb4cd4809d85f", "d7ce20982cead1753b8f52bad2d2c22ba9362d50")
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
gtoken <- config(token = github_token)
req <- GET("https://api.github.com/users/jtleek/repos", gtoken)
stop_for_status(req)
content(req)
req["datasharing"]
req[1]
req[2]
req[[1]]
req[[1]][1]
req[[1]][2]
req[[1]][3]
install.packages("sqldf")
install.packages('RMySQL')
R.version.string
library(swirl)
install_from_swirl("Getting and Cleaning Data")
swirl()
mydf <- read.csv(path2csv, stringsAsFactors=FALSE)
dim(mydf)
head(mydf)
library(dplyr)
packageVersion("dplyr")
cran <- tbl_df(mydf)
rm("mydf")
cran
?select
select(cran,ip_id,package,country)
5:20
select(cran, r_arch:country)
select(cran, country:r_arch)
cran
select(cran,-time)
-5:20
-(5:20)
select(cran, -(X:size))
filter(cran, package == "swirl")
filter(cran, r_version == "3.1.1", country == "US")
?Comparison
filter(cran, r_version <= "3.0.2", country == "IN")
filter(cran, country == "US" | country == "IN")
filter(cran, size > 100500,r_os=="linux-gnu")
is.na(c(3,5,NA,10))
!is.na(c(3,5,NA,10))
filter(cran, !is.na(r_version))
cran2 <- select(cran,size:ip_id)
arrange(cran2,ip_id)
arrange(cran2,desc(ip_id))
arrange(cran2,package, ip_id)
arrange(cran2,country,desc(r_version), ip_id)
cran3 <- select(cran, ip_id, package, size)
cran3
mutate(cran3, size_mb = size / 2^20)
mutate(cran3, size_mb = size / 2^20, size_gb = size_mb/2^10)
mutate(cran3, correct_size = size +1000)
summarize(cran,avg_bytes=mean(size))
library(dplyr)
cran <- tbl_df(mydf)
rm("mydf")
cran
?group_by()
?group_by
by_package <- group_by(cran,package)
by_package
summarize(by_package,mean(size))
submit()
pack_sum
quantile(pack_sum$count, probs=0.99)
top_counts <- filter(pack_sum, count > 679)
top_counts
View(top_counts)
top_counts_sorted <- arrange(top_counts, desc(count))
View(top_counts_sorted)
quantile(pack_sum$unique, probs = 0.99)
top_unique <- filter(pack_sum, unique > 465)
View(top_unique)
top_unique_sorted <- arrange(top_unique, desc(unique))
View(top_unique_sorted)
submit()
submit()
submit()
View(result3)
submit()
submit()
submit()
submit()
library(tidyr)
students
?gather
gather(students, sex, count, -grade)
students2
res <- gather(students2, sex_class, count, -grade)
res
?separate
separate(res, sex_class, c("sex","class"))
submit()
students3
submit()
?spread
submit()
extract_numeric("class5")
?mutate
submit()
students4
submit()
submit()
submit()
passed
failed
passed <- mutate(passed, status = "passed")
failed <- mutate(failed, status = "failed")
bind_rows(passed, failed)
sat
?select
submit()
submit()
?with
?devices
?Devices
?strptime
z <- strptime("20/2/06 11:16:16.683", "%d/%m/%y %H:%M:%OS")
z # prints without fractional seconds
op <- options(digits.secs = 3)
z
getwd()
setwd("/home/g/R/workDir/ExploratoryProject1//ExData_Plotting1")
elecPowCons <- read.table("./household_power_consumption.txt")
head(elecPowCons)
?read.table
elecPowCons <- read.table("./household_power_consumption.txt", sep=";")
?read.table
head(elecPowCons)
tail(elecPowCons)
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
tail(elecPowCons)
head(elecPowCons)
str(elecPowCons)
## Draw a new plot and set the color of the bars to red
hist(elecPowCons$Global_active_power, col="red", breaks=12)
## Add a title
title(main = "Global Active Power")
## Add a x - axis label
title(xlab = "Global Active Power (kilowatts)")
## Add a y - axis label
title(ylab = "Frequency")
elecPowCons$Global_active_power[3]
elecPowCons$Global_active_power[3]*2
as.numeric(elecPowCons$Global_active_power[3])*2
as.numeric(elecPowCons$Global_active_power[3])
(elecPowCons$Global_active_power[3])
head(elecPowCons$Global_active_power[3])
head(elecPowCons$Global_active_power
)
?table
table(elecPowCons$Global_active_power)
hist(table(elecPowCons$Global_active_power))
names(elecPowCons)
elecPowCons$Date[1]
strptime(elecPowCons$Date[1])
?strptime
strptime(elecPowCons$Date[1], format="%m/%d/%y")
strptime(elecPowCons$Date[1], format="dd/mm/yyyy")
strptime(elecPowCons$Date[1], format="%d/%m/%y")
elecPowCons$Date <- strptime(elecPowCons$Date, format="%d/%m/%y")
elecPowCons$Date[1]
elecPowCons$Date[1000]
as.Date(elecPowCons$Date[1000])
names(elecPowCons)
elecPowCons$Time[1000]
strptime(elecPowCons$Time[1000], format="%H:%M:%S")
?as.time
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%y"))
elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
head(elecPowCons)
?date
date()
as.Date("2007-02-01")
elecPowCons[elecPowCons$Date == as.Date("2007-02-01"),]
str(elecPowCons$Date)
head(elecPowCons$Date)
tail(elecPowCons$Date)
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
unique(elecPowCons$Date)
(elecPowCons$Date[1])
as.Date(strptime(elecPowCons$Date[1], format="%d/%m/%y"))
as.Date(strptime(elecPowCons$Date[1], format="%dd/%mm/%yyyy"))
as.Date(strptime(elecPowCons$Date[1], format="%dd/%mm/%yy"))
as.Date(strptime(elecPowCons$Date[1], format="%d/%m/%y"))
?strptime
(strptime(elecPowCons$Date[1], format="%d/%m/%y"))
as.Date(elecPowCons$Date[1])
(strptime(elecPowCons$Date[1], format="%d/%m/%Y"))
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
#extract the relevant data; we will only be using data from the dates 2007-02-01 and 2007-02-02
elecPowCons[elecPowCons$Date == as.Date("2007-02-01"),]
head(elecPowCons[elecPowCons$Date == as.Date("2007-02-01"),])
epSubset <- (elecPowCons[elecPowCons$Date == as.Date("2007-02-01") | elecPowCons$Date == as.Date("2007-02-02") ,])
hist(epSubset$Global_active_power, col="red", breaks=12)
hist(table(epSubset$Global_active_power), col="red", breaks=12)
hist(table(epSubset$Global_active_power), col="red", breaks=100)
hist(table(epSubset$Global_active_power), col="red", breaks=50)
hist(table(epSubset$Global_active_power), col="red", breaks=6)
hist(table(epSubset$Global_active_power), col="red", breaks=12)
hist(table(epSubset$Global_active_power), col="red", breaks=24)
hist(table(epSubset$Global_active_power), col="red", breaks=11)
table(epSubset$Global_active_power)
epSubset$Global_active_power[epSubset$Global_active_power=="?"] <- ""
?NA
#load the dataset
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
#extract the relevant data; we will only be using data from the dates 2007-02-01 and 2007-02-02
epSubset <- (elecPowCons[elecPowCons$Date == as.Date("2007-02-01") | elecPowCons$Date == as.Date("2007-02-02") ,])
#remove all missing values
epSubset$Global_active_power[epSubset$Global_active_power=="?"] <- NA
str(epSubset$Global_active_power)
str(epSubset$Global_active_power=="?")
sum(str(epSubset$Global_active_power=="?"))
hist(epSubset$Global_active_power, col="red", breaks=12)
hist(table(epSubset$Global_active_power), col="red", breaks=12)
hist(table(epSubset$Global_active_power), col="red", breaks=12, na.rm=TRUE)
hist(table(epSubset$Global_active_power[!is.na(epSubset$Global_active_power)]), col="red", breaks=12)
table(epSubset$Global_active_power[!is.na(epSubset$Global_active_power)])
as.numeric(epSubset$Global_active_power)
as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
epSubset$Global_active_power <- as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
#This code assumes you are in the correct working directory
#The working directory should contain the data set from UCI
#load the dataset
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
#extract the relevant data; we will only be using data from the dates 2007-02-01 and 2007-02-02
epSubset <- (elecPowCons[elecPowCons$Date == as.Date("2007-02-01") | elecPowCons$Date == as.Date("2007-02-02") ,])
as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
sum(is.na(epSubset$Global_active_power))
#convert factor levels to numeric levels
epSubset$Global_active_power <- as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
sum(is.na(epSubset$Global_active_power))
hist(epSubset$Global_active_power, col="red", breaks=12)
title(main = "Global Active Power")
## Add a x - axis label
title(xlab = "Global Active Power (kilowatts)")
## Add a y - axis label
title(ylab = "Frequency")
?hist
hist(epSubset$Global_active_power, col="red", breaks=12, main="Global Active Power", xlab="Global Active Power (kilowatts)", ylab="Frequency")
## Copy my plot to a PNG file
dev.copy(png, file = "plot1.png")
# Close the PNG device
dev.off()
elecPowCons$TimeStamp <- format(as.POSIXct(paste(elecPowCons$Date, elecPowCons$Time)), "%d/%m/%Y %H:%M:%S")
head(elecPowCons$TimeStamp)
head(elecPowCons$TimeStamp,20)
head(elecPowCons)
past(elecPowCons$Date,elecPowCons$Time)
paste(elecPowCons$Date[1],elecPowCons$Time[1])
elecPowCons$Date[1]
elecPowCons$Time[1]
#load the dataset
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
strptime(elecPowCons$Time, format="%H:%M:%S")
?strptime
head(elecPowCons$Time)
strptime(paste(elecPowCons$Date,elecPowCons$Time), format="%H:%M:%S")
strptime(paste(elecPowCons$Date[1],elecPowCons$Time[1]), format="%H:%M:%S")
strptime(paste(elecPowCons$Date[1],elecPowCons$Time[1]), format="%y-%m-%d %H:%M:%S")
elecPowCons$Date[1]
strptime(paste(elecPowCons$Date[1],elecPowCons$Time[1]), format="%Y-%m-%d %H:%M:%S")
#load the dataset
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
#elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
elecPowCons$TimeStamp <- strptime(paste(elecPowCons$Date,elecPowCons$Time), format="%Y-%m-%d %H:%M:%S")
head(elecPowCons)
#extract the relevant data; we will only be using data from the dates 2007-02-01 and 2007-02-02
epSubset <- (elecPowCons[elecPowCons$Date == as.Date("2007-02-01") | elecPowCons$Date == as.Date("2007-02-02") ,])
#convert factor levels to numeric levels
epSubset$Global_active_power <- as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
plot(elecPowCon$timestamp, elecPowCon$Global_active_power, type="l")
plot(epSubset$timestamp, epSubset$Global_active_power, type="l")
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l")
?plot
R
$ R
$R
sys.getlocale()
sys
?rconsole
?
Rconsole
Sys.setenv(LANGUAGE="en_US.utf8")
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l")
Sys.setenv(LANGUAGE="en_US.utf8")
Sys.setenv(LANGUAGE="en_US.utf8")
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l")
elecPowCons$TimeStamp
?weekday()
?weekday
?wday
library('lubridate')
wday(elecPowCons$TimeStamp)
?wday
wday(elecPowCons$TimeStamp, label=TRUE)
elecPowCons$weekday <- wday(elecPowCons$TimeStamp, lable=TRUE)
elecPowCons$weekday <- wday(elecPowCons$TimeStamp, label=TRUE)
plot(epSubset$weekday, epSubset$Global_active_power, type="l")
epSubset$weekday <- wday(epSubset$TimeStamp, label=TRUE)
## Draw a new plot and set the color of the bars to red, add labels and title
plot(epSubset$weekday, epSubset$Global_active_power, type="l")
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l")
?plot
?axis
plot(1:10, xaxt = "n")
axis(1, xaxp = c(2, 9, 7))
plot(1:10, xaxt = "n")
axis(1, xaxp = c(2, 9, 5))
?axis
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l", xaxt = "n")
axis(1, xaxp=c("Thu","Fri","Sat"))
axis(1, at=c("Thu","Fri","Sat"))
?axis
axis(1, 1:7, LETTERS[1:7], col.axis = "blue")
?axisz
?axisµ
?axis
plot(1:7, rnorm(7), main = "axis() examples",
type = "s", xaxt = "n", frame = FALSE, col = "red")
axis(1, 1:7, LETTERS[1:7], col.axis = "blue")
plot(1:7, rnorm(7), main = "axis() examples",
type = "s", xaxt = "n", frame = FALSE, col = "red")
Sys.setenv(LANGUAGE="en_US.utf8")
?Sys.setlocale
Sys.getlocale()
Sys.setlocale("LC_TIME", "en")
Sys.setlocale("LC_TIME", "en_US.UTF-8")
Sys.getlocale()
#This code assumes you are in the correct working directory
#The working directory should contain the data set from UCI
#load the dataset
elecPowCons <- read.table("./household_power_consumption.txt", sep=";", header=TRUE)
#convert Date and time variables to R date and time variables
elecPowCons$Date <- as.Date(strptime(elecPowCons$Date, format="%d/%m/%Y"))
#elecPowCons$Time <- strptime(elecPowCons$Time, format="%H:%M:%S")
elecPowCons$TimeStamp <- strptime(paste(elecPowCons$Date,elecPowCons$Time), format="%Y-%m-%d %H:%M:%S")
#extract the relevant data; we will only be using data from the dates 2007-02-01 and 2007-02-02
epSubset <- (elecPowCons[elecPowCons$Date == as.Date("2007-02-01") | elecPowCons$Date == as.Date("2007-02-02") ,])
#my system language is set to a different language. To get the x-axis label right, create the weekday variable
#Sys.setlocale("LC_TIME", "en_US.UTF-8")
#epSubset$weekday <- wday(epSubset$TimeStamp, label=TRUE)
#convert factor levels to numeric levels
epSubset$Global_active_power <- as.numeric(levels(epSubset$Global_active_power))[epSubset$Global_active_power]
## Draw a new plot and set the color of the bars to red, add labels and title
plot(epSubset$TimeStamp, epSubset$Global_active_power, type="l")