forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
156 lines (104 loc) · 4.54 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
149
150
151
152
153
154
155
156
# Reproducible Research: Peer Assessment 1
## Loading and preprocessing the data
```{r}
library(lubridate)
library(lattice)
if (!file.exists("activity.zip")) {
download.file(url = "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",
destfile = "activity.zip",
method = "curl")
}
if (!file.exists("activity.csv")) {
unzip ("activity.zip")
}
activity <- read.csv("activity.csv")
activity[, "clean_date"] <- as.Date(strptime(activity[, "date"], format='%Y-%m-%d'))
timestamp <- strptime(sprintf("%s %04d",
activity[,"date"],
activity[,"interval"]),
format="%Y-%m-%d %H%M")
time <- strftime(timestamp, format="%H:%M")
dayOfWeek <- weekdays(timestamp)
activity <- cbind(activity, timestamp, time, dayOfWeek)
stepsByDay <- aggregate(activity$steps, by=list(activity$date), FUN=sum, na.rm=TRUE)
names(stepsByDay) <- c("date", "steps")
stepsByInterval = aggregate(activity$steps, by=list(activity$interval), FUN=mean, na.rm=TRUE)
names(stepsByInterval) <- c("interval", "steps")
stepsByTime = aggregate(activity$steps, by=list(activity$time), FUN=mean, na.rm=TRUE)
names(stepsByTime) <- c("timestamp", "steps")
```
## What is mean total number of steps taken per day?
```{r histogram, fig.height=6}
hist(stepsByDay$steps,
breaks = 20,
main = "Histogram of total steps taken per day",
xlab = "Total Steps")
```
```{r mean_median}
totalStepsMean <- round(mean(stepsByDay$steps, na.rm=TRUE))
totalStepsMedian <- median(stepsByDay$steps, na.rm=TRUE)
```
The mean number of steps taken per day is `r totalStepsMean`.
The median number of steps taken per day is `r totalStepsMedian`.
## What is the average daily activity pattern?
```{r dailyPlot, fig.height=6}
plot(stepsByTime$timestamp,
stepsByTime$steps,
type="l",
main="Daily Activity Pattern",
xlab = "Time",
ylab = "Steps")
```
```{r maxSteps}
maxStepRow <- activity[which(activity[, 1] == max(activity$steps, na.rm = TRUE)), ]
```
The maximum number of steps in a 5 minute interval was taken on `r maxStepRow$date` during the interval starting at `r maxStepRow$time`
## Imputing missing values
```{r missingValues}
naCount <- sum(is.na(activity$steps))
```
There are `r naCount` missing step values in the data set.
I will fill in the missing values with the average number of steps taken in that time interval on the days where there is data reported.
```{r fillInMissingValues}
row.names(stepsByInterval) = stepsByInterval$interval
activityExtrapolated <- activity
for (i in 1:nrow(activityExtrapolated)) {
if (is.na(activityExtrapolated[i,"steps"])) {
interval <- toString(activityExtrapolated[i, "interval"])
activityExtrapolated[i,"steps"] <- stepsByInterval[interval, "steps"]
}
}
stepsByDayExtrapolated <- aggregate(activityExtrapolated$steps,
by=list(activityExtrapolated$date),
FUN=sum)
names(stepsByDayExtrapolated) <- c("date", "steps")
```
```{r histogramWithExtrapolated, fig.height=6}
hist(stepsByDayExtrapolated$steps,
breaks = 20,
main = "Histogram of total steps taken per day",
xlab = "Total Steps")
```
```{r mean_median_extrapolated}
totalStepsExtrapolatedMean <- round(mean(stepsByDayExtrapolated$steps))
totalStepsExtrapolatedMedian <- median(stepsByDayExtrapolated$steps)
```
The mean number of steps taken per day is `r totalStepsExtrapolatedMean`.
The median number of steps taken per day is `r totalStepsExtrapolatedMedian`.
These values are higher than the estimates in the first part. Adding in the missing data increased the overal mean and median.
## Are there differences in activity patterns between weekdays and weekends?
```{r weekdays_weekends}
activityExtrapolated[,"day_factor"] <- ifelse(activityExtrapolated$dayOfWeek == "Saturday" |
activityExtrapolated$dayOfWeek == "Sunday",
"Weekend", "Weekday")
weekdays <- activityExtrapolated[which(activityExtrapolated$day_factor == "Weekday"),]
weekends <- activityExtrapolated[which(activityExtrapolated$day_factor == "Weekend"),]
byDayType <- aggregate(activityExtrapolated$steps,
by=list(activityExtrapolated$interval,
activityExtrapolated$day_factor),
FUN=mean)
names(byDayType) <- c("interval", "dayType", "steps")
```
```{r dayTypePlot, fig.height=6}
xyplot(steps ~ interval | dayType, data = byDayType, type = "l", layout=c(1,2))
```