forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 16.5 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
) %>% print
sat1 <- sat %>%
select(-contains("total")) %>%
gather(part_sex, count, -score_range) %>%
separate(part_sex, c("part", "sex")) %>%
group_by(score_range, sex) %>%
mutate(total = sum(count),
test = mean(count),
prop = count / total
test.prop = median(count)
) %>% print
sat1 <- sat %>%
select(-contains("total")) %>%
gather(part_sex, count, -score_range) %>%
separate(part_sex, c("part", "sex")) %>%
group_by(score_range, sex) %>%
mutate(total = sum(count),
test = mean(count),
prop = count / total,
test.prop = median(count)
) %>% print
swirl()
swirl()
library(swirl)
swirl()
library(tidyr)
students
?gather
gather(students, sex, count, -grade)
students2
res <- gather(students2, sex_class, count)
res <- gather(students2, sex_class, count, -grade)
res
?separate
separate(res, sex_class, c("sex", "class"))
submit()
submit()
students3
submit()
?spread
submit()
extract_numeric("class5")
submit()
submit()
students4
submit()
submit()
submit()
passed
failed
mutate(passed, status = "passed")
passed <- passed %>% mutate(status = "passed")
failed <- failed %>% mutate(status = "failed")
?rbind_list()
rbind_list(passed, failed)
swirl
swirl()
library(swirl)
swirl()
?help
help()
ljadfj
sat
submit()
submit()
exit()
end()
bye()
swirl()
sat
submit()
submit(0)
submit()
submit()
submit()
Sys.getlocale("LC_TIME")
library(lubridate)
help(package = lubridate)
today()
this_day <- today()
this_day
year(this_day)
wday(this_day)
wday(this_day, label = TRUE)
now()
this_moment <- now()
this_moment
seconds(this_moment)
second(this_moment)
ymd("1989-05-17")
my_date <- ymd("1989-05-17")
my_date
class(my_date)
ymd('1989 May 17')
mdy("March 12, 1975")
dmy(25081985)
ymd("192012")
ymd("1920//12")
ymd("1920/1/2")
dt1
ymd_hms(dt1)
hms("03:22:14")
dt2
ymd(dt2)
update(this_moment, hours = 8, minutes = 34, seconds = 55)
this_moment
this_moment <- update(hours = 9, minutes = 50)
this_moment <- update(this_moment, hours = 9, minutes = 50)
this_moe
this_moment
?now()
nyc <- now(tzone = "America/New_York")
nyc
depart <- nyc + days(2)
depart
depart <- update(depart, hours = 17, minutes = 34)
depar
depart
arrive <- nyc + hours(15) + minutes(50)
arrive <- depart + hours(15) + minutes(50)
?with_tz
arrive <- with_tz(arrive, tzone = "Asia/Hong_Kong")
arrive
mdy("June 17, 2008")
mdy("June 17, 2008", tz = "Singapore")
last_time <- mdy("June 17, 2008", tz = "Singapore")
last_time
?new_interval
how_long <- new_interval(arrive, last_time)
how_long <- new_interval(last_time, arrive)
as.period(how_long)
stopwatch()
library('lubridate')
library('ggplot2')
library('dplyr')
Load the following packages. If packages have not been installed, you will need to install them at this point using install.packages().
```{r, results= 'hide'}
library('lubridate')
library('ggplot2')
library('dplyr')
```
#### Load data
``` {r, results='hide'}
data <- read.csv('activity.csv')
```
## Steps per day analysis
```{r, echo=FALSE}
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90))
```
**Rearrange data for calculations**
```{r, results='hide'}
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
```
**Mean and median calculation**
```{r, mean, echo=FALSE}
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
```
The mean steps per day is **`r mean.steps.day`**.
The median steps per day is **`r median.steps.day`**.
## Average daily pattern analysis
In order to plot the average number of steps taken taken per day, averaged across all days, the data must be rearranged.
```{r, results = 'hide'}
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
```
Now data is ready to be plotted with ggplot
```{r}
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval')
```
setwd("~/Documents/github_repositories/reproducible_research/project_1")
data <- read.csv('activity.csv')
library('lubridate')
library('ggplot2')
library('dplyr')
## part 1: plot steps per day
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90))
## rearrange data for calculations
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
## mean calculation
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
## median calculation
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval')
complete.cases(data)
sum(complete.cases(data))
sum(!omplete.cases(data))
sum(!complete.cases(data))
inc.cases <- sum(!complete.cases(data))
?axis
break.vec <- c(min(data$date, median(data$date, max(data$date))))
break.vec <- c(min(data$date), median(data$date), max(data$date))))
break.vec <- c(min(data$date), median(data$date), max(data$date))
class(data$date)
View(data)
break.vec <- c(min(as.Date(data$date), median(as.Date(data$date), max(as.Date(data$date)))
break.vec <- c(min(as.Date(data$date), median(as.Date(data$date)), max(as.Date(data$date))))
break.vec <- c(min(as.Date(data$date)), median(as.Date(data$date)), max(as.Date(data$date))))
break.vec <- c(min(as.Date(data$date)), median(as.Date(data$date)), max(as.Date(data$date)))
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(breaks = break.vec)
plot1
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
scale_x_date(breaks = break.vec) +
theme(axis.text.x = element_text(angle = 90))
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(labels = date_format('%m'))
?scale_x_date
data$date <- as.POSIXct(data$date)
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(labels = date_format('%w'))
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(labels = date_format('%w'))
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(breaks = date_breaks('week'))
library('scales')
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(breaks = date_breaks('week'))
?scale_x
?scale_x_date
ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
data$date <- as.Date(data$date)
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
detach("package:scales", unload=TRUE)
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
## rearrange data for calculations
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
## mean calculation
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
## median calculation
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
data$date <- as.Date(data$date)
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
## rearrange data for calculations
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
## mean calculation
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
## median calculation
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
data <- read.csv('activity.csv')
library('lubridate')
library('ggplot2')
library('dplyr')
data$date <- as.Date(data$date)
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
## rearrange data for calculations
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
## mean calculation
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
## median calculation
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
View(tot.steps.day)
tot.steps.day <- data %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
mean.steps.day <- mean(tot.steps.day$tot.steps, na.rm = TRUE)
median.steps.day <- median(tot.steps.day$tot.steps, na.rm = TRUE)
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
scale_x_date()
plot2
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
str(data$date)
data$date <- as.POSIXct(data$date)
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date()
plot1
plot1 <- ggplot(data, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_datetime()
plot1
plot4 <- ggplot(data3, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_datetime()
plot4
plot4 <- ggplot(data3, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_datetime()
int.mean <- data %>%
group_by(interval) %>%
summarise(mean.steps = mean(steps, na.rm = TRUE))
plot2 <- ggplot(int.mean, aes(x = interval, y = mean.steps)) +
theme(legend.position = 'none') +
geom_line(aes(color = mean.steps), size = 1.4) +
geom_hline(aes(yintercept = mean(int.mean$mean.steps))) +
annotate('text', x = 4, y = 45, label = 'mean') +
labs(title = 'Mean Steps Per Interval') +
plot2
## interval with max mean steps per day
max.int.steps <- filter(int.mean, mean.steps == max(mean.steps))
## part 3
inc.cases <- sum(!complete.cases(data))
data3 <- data
data3 <- left_join(data3, int.mean, by = 'interval')
data3$steps[which(is.na(data3$steps))] <- data3$mean.steps[is.na(data3$steps)]
plot4 <- ggplot(data3, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_datetime()
plot4
plot4 <- ggplot(data3, aes(x = date, y = steps)) +
geom_histogram(stat = 'identity') +
labs(title = "Steps Per Day", x = 'Date', y = 'Number of Steps') +
scale_x_datetime()
plot4
filter(int.mean, mean.steps == max(mean.steps))
max.int.steps <-filter(int.mean, mean.steps == max(mean.steps))
max.int.steps$interval
tot.steps.day.data3 <- data3 %>%
group_by(date) %>%
summarise(tot.steps = sum(steps))
imp.mean <- mean(tot.steps.day.data3$tot.steps)
imp.median <-median(tot.steps.day.data3$tot.steps)
imp.mean
imp.median
## part 4
data4 <- data3
data4$wday <- wday(data4$date)
length(which(data4$wday >5))
length(which(data4$wday <= 5))
data4$week.cat[which(data4$wday > 5)] <- 'weekend'
data4$week.cat[is.na(data4$week.cat)] <- 'weekday'
data4$week.cat <- as.factor(data4$week.cat)
int.data4 <- data4 %>%
group_by(week.cat, interval) %>%
summarise(mean.steps = mean(steps))
data4 <- data3
data4$wday <- wday(data4$date)
length(which(data4$wday >5))
length(which(data4$wday <= 5))
data4$week.cat[which(data4$wday > 5)] <- 'weekend'
str(data4)
rm(data4)
str(data3)
data4$wday <- wday(data4$date)
data4 <- data3
data4$wday <- wday(data4$date)
data4$week.cat[which(data4$wday > 5)] <- 'weekend'
table(data4$wday)
data4$week.cat <- NA
data4$week.cat[which(data4$wday > 5)] <- 'weekend'
data4$week.cat[is.na(data4$week.cat)] <- 'weekday'
data4$week.cat <- as.factor(data4$week.cat)
table(data4$week.cat)
int.data4 <- data4 %>%
group_by(week.cat, interval) %>%
summarise(mean.steps = mean(steps))
View(int.data4)
plot5 <- ggplot(int.data4, aes(x = interval, y = mean.steps, group = week.cat)) +
geom_line() + facet_wrap(~week.cat, ncol = 1)
plot5
plot5 <- ggplot(int.data4, aes(x = interval, y = mean.steps, group = week.cat)) +
geom_line() + facet_wrap(~week.cat, ncol = 1) +
labs(tittle = 'Weekday vs weekend: mean steps per internval', x = 'Interval', y = 'Mean steps')
plot5
plot5 <- ggplot(int.data4, aes(x = interval, y = mean.steps, group = week.cat)) +
geom_line() + facet_wrap(~week.cat, ncol = 1) +
labs(title = 'Weekday vs weekend: mean steps per internval', x = 'Interval', y = 'Mean steps')
plot5
?knitr
library('knitr')
?knit2html
install.packages("markdown")
library("markdown", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
library("rmarkdown", lib.loc="/Library/Frameworks/R.framework/Versions/3.1/Resources/library")
library('markdown')
detach("package:rmarkdown", unload=TRUE)