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
83 lines (63 loc) · 2.27 KB
/
PA1_template.Rmd
File metadata and controls
83 lines (63 loc) · 2.27 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## 1. Code for reading in the dataset and/or processing the data
```{r}
unzip("activity.zip")
data <- read.csv("activity.csv")
```
## 2. Histogram of the total number of steps taken each day
```{r}
data$date <- as.Date(data$date, "%Y-%m-%d")
stepsTotal <- tapply(data$steps, data$date, sum)
hist(stepsTotal, col="blue", xlab = "Total Steps per Day", ylab = "Frequency",
main = "Histogram of the total number of steps taken each day")
```
## 3. Mean and median number of steps taken each day
```{r}
mean(stepsTotal, na.rm = TRUE)
median(stepsTotal, na.rm = TRUE)
```
## 4. Time series plot of the average number of steps taken
```{r}
stepsMean <- tapply(data$steps, data$interval, mean, na.rm = TRUE)
plot(col="blue", row.names(stepsMean), stepsMean, type="l",
xlab="Time Interval", ylab="Mean number of steps taken",
main="Time series plot of the average number of steps taken")
```
## 5. The 5-minute interval that, on average, contains the maximum number of steps
```{r}
intervalStepsMax <- names(which.max(stepsMean))
intervalStepsMax
```
## 6. Code to describe and show a strategy for inputing missing data
```{r}
naIndices <- which(is.na(data))
values <- stepsMean[as.character(data[naIndices, 3])]
names(values) <- naIndices
for (i in naIndices) {
data$steps[i] = values[as.character(i)]
}
stepsTotal <- tapply(data$steps, data$date, sum)
```
## 7. Histogram of the total number of steps taken each day after missing values are imputed
```{r}
hist(stepsTotal, col = "blue", xlab = "Total Steps per Day",
ylab = "Frequency", main = "Histogram of the total number of steps without NAs")
```
## 8. Panel plot comparing the average number of steps taken per 5-minute interval across weekdays and weekends
```{r echo = FALSE}
locale <- Sys.setlocale("LC_TIME", "C")
```
```{r}
days <- weekdays(data$date)
data$dayKind <- ifelse(days == "Saturday" | days == "Sunday", "Weekend", "Weekday")
stepsDays <- aggregate(data$steps, by = list(data$interval, data$dayKind), mean)
names(stepsDays) <- c("interval", "dayKind", "steps")
library(lattice)
xyplot(steps ~ interval | dayKind, stepsDays, type = "l", layout = c(1,2),
xlab = "Interval", ylab = "Number of steps")
```