forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
148 lines (112 loc) · 5.19 KB
/
PA1_template.Rmd
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
---
title: "Reproducible Research Course Project 1"
author: "Ziwen Li"
date: "10/10/2019"
output:
html_document:
keep_md: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<style>
div.blue pre { background-color:lightgrey; }
div.blue pre.r { background-color:#B2EF54; }
</style>
### Loading and preprocessing the data
Load and look at the data.
<div class = "blue">
```{r}
old <- read.csv("~/Downloads/activity.csv")
head(old)
summary(old)
```
</div>
It is clear that there are a lot of missing values in the data, which needs to be dealt with later.
### Question 1 What is mean total number of steps taken per day?
For this part of the assignment, I will ignore the missing values in the dataset as instructed.
Calculate the total number of steps taken per day using dplyr.
<div class = "blue">
```{r message = FALSE}
library(dplyr)
summary_old1 <- old %>% group_by(date) %>% summarise(total = sum(steps))
head(summary_old1)
```
</div>
Make a histogram of the total number of steps taken each day.
<div class = "blue">
```{r fig.height = 6, fig.width = 6}
hist(summary_old1$total, main = "Histogram of the total number of steps taken each day", xlab = "total number of steps", col = "orange")
```
</div>
Calculate and report the mean and median of the total number of steps taken per day.
<div class = "blue">
```{r}
summary(summary_old1$total)
```
</div>
**The mean and median of the total number of steps taken per day are 10766 and 10765 respectively.**
### Question 2 What is the average daily activity pattern?
Make a time series plot (type = "l") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis).
<div class = "blue">
```{r}
summary_old2 <- old %>% group_by(interval) %>% summarise(average = mean(steps, na.rm = TRUE))
head(summary_old2)
summary(summary_old2)
with(summary_old2, plot(x = interval, y = average, type = "l", lwd = 2, col = "orange", xlab = "5-minute interval", ylab = "average number of steps taken across all days"))
```
</div>
### Imputing missing values
Note that there are a number of days/intervals where there are missing values, NAs. The presence of missing days may introduce bias into some calculations or summaries of the data.
Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs.)
<div class = "blue">
```{r}
no_missing_row <- nrow(old[!complete.cases(old), ])
no_missing_row
```
</div>
**The total number of rows with NAs is 2304.**
Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day or the mean for that 5-minute interval, etc.
I decided to use the mean for that 5-minute interval (data from summary_old2). Firstly, I split the data into two data frames, 'complete' and 'missing'. Then I replaced the NAs in 'missing' with the mean for 5-minute interval. Then I combined the altered 'missing' data frame and 'complete' data frame to create a new dataset 'new' with the missing data filled in.
<div class = "blue">
```{r}
complete <- old[complete.cases(old), ]
missing <- old[!complete.cases(old), ]
missing$steps[missing$interval %in% summary_old2$interval] <- summary_old2$average
new <- rbind(complete, missing)
```
</div>
Make a histogram of the total number of steps taken each day and calculate and report the mean and median total number of steps taken per day.
<div class = "blue">
```{r fig.height = 6, fig.width = 6}
summary_new <- new %>% group_by(date) %>% summarise(total = sum(steps))
head(summary_new)
hist(summary_new$total, main = "New histogram of the total number of steps taken each day", xlab = "total number of steps", col = "darkorange")
```
</div>
Calculate and report the mean and median of the total number of steps taken per day.
<div class = "blue">
```{r}
summary(summary_new$total)
```
</div>
**The mean and median of the total number of steps taken per day are 10766 and 10766 respectively.**
### Question 3 Are there differences in activity patterns between weekdays and weekends?
For this part the weekdays() function is used. The dataset with the filled-in missing values, 'new', is used.
Create a new factor variable in the dataset with two levels - "weekday" and "weekend" indicating whether a given date is a weekday or weekend day.
<div class = "blue">
```{r}
new_week <- new %>% mutate(day = weekdays(as.Date(date))) %>% mutate (day = case_when(day %in% c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") ~ "weekday", day %in% c("Saturday", "Sunday") ~ "weekend"))
head(new_week)
```
</div>
Make a panel plot containing a time series plot (i.e. type = "l") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis).
<div class = "blue">
```{r message = FALSE}
summary_new_week <- new_week %>% group_by(interval, day) %>% summarise(average = mean(steps, na.rm = TRUE))
head(summary_new_week)
summary(summary_new_week)
library(lattice)
with(summary_new_week, xyplot(average ~ interval|day, type = "l", lwd = 2, col = "darkorange", xlab = "5-minute interval", ylab = "average number of steps taken across all days", layout= c(1, 2)))
```
</div>