forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PA1_template.Rmd
100 lines (69 loc) · 2.47 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
```{r}
library(dplyr)
library(xtable)
library(lattice)
library(lubridate)
data <- read.csv("activity.csv")
```
## What is mean total number of steps taken per day?
```{r}
stepsPerDay <- data %>%
group_by(date) %>%
summarize(tot = sum(steps, na.rm = T))
hist(stepsPerDay$tot, main = "Histogram of Steps per Day", xlab = "Steps per Day")
dtMean <- round(mean(stepsPerDay$tot),digits = 2)
dtMedian <- median(stepsPerDay$tot)
```
#### Mean: `r as.character(dtMean)`
#### Median: `r dtMedian`
## What is the average daily activity pattern?
```{r}
stepsPerTime <- data %>%
group_by(interval) %>%
summarize(avg = mean(steps, na.rm=T))
plot(stepsPerTime$interval,stepsPerTime$avg,type = "l",main="Avg Steps per Interval",xlab="5 min interval",ylab="Avg Steps")
dtMax <- filter(stepsPerTime, avg == max(stepsPerTime$avg)) %>% select(interval)
```
#### Interval with highest average steps: `r dtMax`
## Imputing missing values
I took the interval's average in order to fill in NA values. A better method might be
to break intervals down by day type
```{r}
NArows <- data %>%
filter(is.na(steps))
naRowCount <- count(NArows)
```
#### Rows with NA: `r naRowCount`
```{r}
nonNArows <- data %>%
filter(!is.na(steps))
nonNArows$steps <- as.numeric(nonNArows$steps)
avgData <- select(NArows, -steps) %>%
inner_join(stepsPerTime) %>%
select(avg, date, interval)
colnames(avgData) <- colnames(nonNArows)
fullData <- dplyr::union(nonNArows,avgData)
stepsPerDayFull <- fullData %>%
group_by(date) %>%
summarize(tot = sum(steps, na.rm = T))
hist(stepsPerDayFull$tot, main = "Histogram of Steps per Day w/ Avg for NAs", xlab = "Steps per Day")
dtMean <- round(mean(stepsPerDay$tot),digits = 2)
dtMedian <- median(stepsPerDay$tot)
```
#### Mean w/ Interval Avg for NAs: `r as.character(dtMean)`
#### Median w/ Interval Avg for NAs: `r dtMedian`
## Are there differences in activity patterns between weekdays and weekends?
```{r}
fullData <- mutate(fullData,dayType = ifelse(wday(as.Date(fullData$date),T) == c("Sun","Sat"),"Weekend","Weekday")) %>%
group_by(dayType, interval) %>%
summarize(avg = mean(steps)) %>%
arrange(interval)
xyplot(avg ~ interval | dayType, fullData, type="l", layout = c(1,2), xlab = "Interval", ylab="Average Steps",main="Average Steps by Day Type")
````