forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA1_template.html
More file actions
315 lines (224 loc) · 7.35 KB
/
PA1_template.html
File metadata and controls
315 lines (224 loc) · 7.35 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
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Loading and preprocessing the data</title>
<script type="text/javascript">
window.onload = function() {
var imgs = document.getElementsByTagName('img'), i, img;
for (i = 0; i < imgs.length; i++) {
img = imgs[i];
// center an image if it is the only element of its parent
if (img.parentElement.childElementCount === 1)
img.parentElement.style.textAlign = 'center';
}
};
</script>
<style type="text/css">
body, td {
font-family: sans-serif;
background-color: white;
font-size: 13px;
}
body {
max-width: 800px;
margin: auto;
padding: 1em;
line-height: 20px;
}
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
h1 {
font-size:2.2em;
}
h2 {
font-size:1.8em;
}
h3 {
font-size:1.4em;
}
h4 {
font-size:1.0em;
}
h5 {
font-size:0.9em;
}
h6 {
font-size:0.8em;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
pre, img {
max-width: 100%;
}
pre {
overflow-x: auto;
}
pre code {
display: block; padding: 0.5em;
}
code {
font-size: 92%;
border: 1px solid #ccc;
}
code[class] {
background-color: #F8F8F8;
}
table, td, th {
border: none;
}
blockquote {
color:#666666;
margin:0;
padding-left: 1em;
border-left: 0.5em #EEE solid;
}
hr {
height: 0px;
border-bottom: none;
border-top-width: thin;
border-top-style: dotted;
border-top-color: #999999;
}
@media print {
* {
background: transparent !important;
color: black !important;
filter:none !important;
-ms-filter: none !important;
}
body {
font-size:12pt;
max-width:100%;
}
a, a:visited {
text-decoration: underline;
}
hr {
visibility: hidden;
page-break-before: always;
}
pre, blockquote {
padding-right: 1em;
page-break-inside: avoid;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
@page :left {
margin: 15mm 20mm 15mm 10mm;
}
@page :right {
margin: 15mm 10mm 15mm 20mm;
}
p, h2, h3 {
orphans: 3; widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}
</style>
</head>
<body>
<h2>Loading and preprocessing the data</h2>
<p>The data is stored in a zipped CSV file, we'll load it in to the "data" variable. We'll then transform the data into a data.table for easier use.</p>
<pre><code class="r">library(data.table)
</code></pre>
<pre><code>## data.table 1.9.4 For help type: ?data.table
## *** NB: by=.EACHI is now explicit. See README to restore previous behaviour.
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:lubridate':
##
## hour, mday, month, quarter, wday, week, yday, year
</code></pre>
<pre><code class="r">originaldata <- data.table(read.csv(unz("activity.zip", "activity.csv")))
</code></pre>
<p>Exclude all of the rows that have NA for the steps column</p>
<pre><code class="r">data <- originaldata[!is.na(originaldata$steps), ]
data <- originaldata
</code></pre>
<h2>What is mean total number of steps taken per day?</h2>
<p>Transform the data into daily totals by summing up the number of steps by day, using the aggregate function. </p>
<pre><code class="r">dailySteps <- aggregate(steps ~ date, data, sum)
</code></pre>
<p>Create a histogram out of the total number of steps taken in a day.</p>
<pre><code class="r">hist(dailySteps$steps, xlab = "Daily Steps", main = "Steps taken in a day")
</code></pre>
<p><img src="figure/unnamed-chunk-4-1.png" alt="plot of chunk unnamed-chunk-4"> </p>
<p>Take the results and calculate the mean and median of the "steps" variable.</p>
<pre><code class="r">mean(dailySteps$steps)
</code></pre>
<pre><code>## [1] 10766.19
</code></pre>
<pre><code class="r">median(dailySteps$steps)
</code></pre>
<pre><code>## [1] 10765
</code></pre>
<h2>What is the average daily activity pattern?</h2>
<p>Transform the data into average steps taken in each five minute interval, using the mean.</p>
<pre><code class="r">intervalSteps <- aggregate(steps ~ interval, data, mean)
</code></pre>
<p>Create a time series plot.</p>
<pre><code class="r">plot(x = intervalSteps$interval, y = intervalSteps$steps, type = "l", xlab = "Interval", ylab = "Steps", main = "Daily Activity Pattern")
</code></pre>
<p><img src="figure/unnamed-chunk-7-1.png" alt="plot of chunk unnamed-chunk-7"> </p>
<p>Use the max function to pull out the 5 minute interval that has the highest average number of steps.</p>
<pre><code class="r">intervalSteps$interval[intervalSteps$steps == max(intervalSteps$steps)]
</code></pre>
<pre><code>## [1] 835
</code></pre>
<h2>Imputing missing values</h2>
<p>Calculate the total number of rows with missing values.</p>
<pre><code class="r">sum(is.na(originaldata))
</code></pre>
<pre><code>## [1] 2304
</code></pre>
<p>Fill in the NA data with the average values</p>
<pre><code class="r">cleanedData <- originaldata
cleanedData$steps[is.na(cleanedData$steps)] <- mean(originaldata$steps)
</code></pre>
<p>Pull out the daily steps by summing up the number of steps by day, using the aggregate function. </p>
<pre><code class="r">dailySteps <- aggregate(steps ~ date, cleanedData, sum)
</code></pre>
<p>Create a histogram out of the total number of steps taken in a day.</p>
<pre><code class="r">hist(dailySteps$steps, xlab = "Daily Steps", main = "Steps taken in a day")
</code></pre>
<p><img src="figure/unnamed-chunk-12-1.png" alt="plot of chunk unnamed-chunk-12"> </p>
<p>Take the results and calculate the mean and median of the "steps" variable.</p>
<pre><code class="r">mean(dailySteps$steps)
</code></pre>
<pre><code>## [1] 10766.19
</code></pre>
<pre><code class="r">median(dailySteps$steps)
</code></pre>
<pre><code>## [1] 10765
</code></pre>
<p>The histogram, mean, and median values did not change at all by imputting the missing values. This is because the imputted values were calculated by using the mean function.</p>
<h2>Are there differences in activity patterns between weekdays and weekends?</h2>
<p>Create a new variable for tracking if the day is a weekday or a weekend. Us the lubridate package to help out.</p>
<pre><code class="r">library(lubridate)
cleanedData$isWeekend <- weekdays(ymd(cleanedData$date)) %in% c("Saturday", "Sunday")
</code></pre>
<p>Convert the data to intervals.</p>
<pre><code class="r">intervalStepsWeekday <- aggregate(steps ~ interval, cleanedData[cleanedData$isWeekend == F, ], mean)
intervalStepsWeekday$weekendSegment = "Weekday"
intervalStepsWeekend <- aggregate(steps ~ interval, cleanedData[cleanedData$isWeekend == T, ], mean)
intervalStepsWeekend$weekendSegment = "Weekend"
intervalStepsFaceted = rbind(intervalStepsWeekend, intervalStepsWeekday)
</code></pre>
<p>Create the plots to compare the data</p>
<pre><code class="r">library(ggplot2)
g <- ggplot(intervalStepsFaceted, aes(interval, steps))
g + geom_line() + facet_wrap(~ weekendSegment, nrow = 2) + labs(x = "Interval") + labs(y = "Number of steps")
</code></pre>
<p><img src="figure/unnamed-chunk-16-1.png" alt="plot of chunk unnamed-chunk-16"> </p>
</body>
</html>