This repository was archived by the owner on Aug 29, 2019. It is now read-only.
forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA1_template.Rmd
More file actions
82 lines (71 loc) · 2.39 KB
/
PA1_template.Rmd
File metadata and controls
82 lines (71 loc) · 2.39 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
```{r}
unzip("activity.zip")
data <- read.csv("activity.csv")
```
## What is mean total number of steps taken per day?
```{r}
total <- aggregate(steps ~ date, data, sum)
hist(total$steps, main="Total number of steps taken each day", xlab="Steps")
```
Mean of total number of steps taken each day:
```{r}
mean(total$steps)
```
Median of total number of steps taken each day:
```{r}
median(total$steps)
```
## What is the average daily activity pattern?
```{r}
avg <- aggregate(steps ~ interval, data, mean)
plot(avg$interval, avg$steps, type="l", main="Average daily activity pattern", xlab="Interval", ylab="Steps")
```
The 5-minute interval which contains the maximum number of steps:
```{r}
avg[which.max(avg$steps),]
```
## Imputing missing values
Total number of rows with missing values na:
```{r}
sum(is.na(data$steps))
```
A new data set will be created where all the na values will be replaced with mean for that 5-minute interval
```{r}
new_data <- data # make a copy of the data
nas <- is.na(new_data$steps) # find all rows with na
avg <- tapply(new_data$steps, new_data$interval, mean, na.rm=TRUE, simplify=TRUE) # calculate the avg of interval
new_data$steps[nas] <- avg[as.character(new_data$interval[nas])] # assign the avg value to the missing value
```
```{r}
new_total <- aggregate(steps ~ date, new_data, sum)
hist(new_total$steps, main="Total number of steps taken each day (with missing values imputed)", xlab="Steps")
```
Mean of total number of steps taken each day (with missing values imputed):
```{r}
mean(new_total$steps)
```
Median of total number of steps taken each day (with missing values imputed):
```{r}
median(new_total$steps)
```
Mean is the same for both data set (with / without imputed missing values), however there is slight difference for the median.
## Are there differences in activity patterns between weekdays and weekends?
```{r}
new_data$daytype <- ifelse(weekdays(as.Date(new_data$date)) %in% c("Saturday", "Sunday"), "weekend", "weekday")
new_data$daytype <- as.factor(new_data$daytype)
```
```{r}
avg <- aggregate(steps ~ interval + daytype, new_data, mean)
library(ggplot2)
ggplot(avg, aes(x=interval, y=steps)) +
facet_grid(daytype ~ .) +
ggtitle("Average daily activity pattern, across all weekday days or weekend days") +
geom_line()
```