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
108 lines (92 loc) · 3.28 KB
/
Copy pathPA1_template.Rmd
File metadata and controls
108 lines (92 loc) · 3.28 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
```{r}
activity<- read.csv("activity.csv")
activity$date<-as.Date(activity$date)
```
## What is mean total number of steps taken per day?
```{r}
aggActivity <- aggregate(steps ~ date, activity, sum)
hist(aggActivity$steps, ylab = "frequency", main="total number of steps per day", xlab="number of steps", breaks=10)
```
Mean of steps per day:
```{r}
meanOfStepsPerDay <-mean(aggActivity$steps)
```
```{r,echo=FALSE}
meanOfStepsPerDay
```
Median of steps per day:
```{r}
medianOfStepsPerDay <- median(aggActivity$steps)
```
```{r,echo=FALSE}
medianOfStepsPerDay
```
## What is the average daily activity pattern?
```{r}
intervalActivity <- aggregate(steps ~ interval, activity, mean)
plot(intervalActivity$interval, intervalActivity$steps, type="l",xlab="time of the day (in minutes)", ylab="number of steps", main= "daily activity pattern")
```
###Interval with the most number of steps:
```{r}
maxInterval <-intervalActivity$interval[which(intervalActivity$steps==max(intervalActivity$steps))]
maxInterval
```
## Imputing missing values
number of missing values:
```{r}
nrow(activity[is.na(activity$steps),])
```
Repacing the missing values by the mean for that interval:
```{r}
cleanedActivity <-activity
for(i in 1:nrow(activity)){
if(is.na(activity$steps[i])){
cleanedActivity$steps[i]<-intervalActivity$steps[which(intervalActivity$interval==activity$interval[i])]
}
}
CleanedaggActivity <- aggregate(steps ~ date, cleanedActivity, sum)
hist(CleanedaggActivity$steps, ylab = "frequency", main="total number of steps per day", xlab="number of steps", breaks=10)
```
Mean of steps per day for cleaned data:
```{r}
CleanedmeanOfStepsPerDay <-mean(CleanedaggActivity$steps)
```
```{r,echo=FALSE}
CleanedmeanOfStepsPerDay
```
Median of steps per day for cleaned data:
```{r}
CleanedmedianOfStepsPerDay <- median(CleanedaggActivity$steps)
```
```{r,echo=FALSE}
CleanedmedianOfStepsPerDay
```
Because the missing values have been replaced by the mean for that interval, the total mean per day doesn't change but the median is closer to the mean.
## Are there differences in activity patterns between weekdays and weekends?
Sorting which dates are weekdays and which are weekend:
```{r}
cleanedActivity$weekday <- weekdays(cleanedActivity$date)
cleanedActivity$weekend[cleanedActivity$weekday %in% c("Saturday","Sunday")]<-"weekend"
cleanedActivity$weekend[!(cleanedActivity$weekday %in% c("Saturday","Sunday"))]<-"weekday"
cleanedActivity$weekend <- factor(cleanedActivity$weekend)
```
Getting the average by interval for weekdays and weekends:
```{r}
WeekendintervalActivity <- aggregate(steps ~ interval, cleanedActivity[cleanedActivity$weekend=="weekend",], mean)
WeekendintervalActivity$weekend<-"weekend"
WeekintervalActivity <- aggregate(steps ~ interval, cleanedActivity[cleanedActivity$weekend=="weekday",], mean)
WeekintervalActivity$weekend<-"weekday"
weekIntervals<- rbind(WeekintervalActivity,WeekendintervalActivity)
weekIntervals$weekend <- factor(weekIntervals$weekend)
```
```{r, fig.width=8}
library(lattice)
xyplot(steps ~ interval|weekend, data=weekIntervals, layout=c(1,2),type="l",main="Comparison of average steps by interveal between work days and weekends")
```