-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.html
111 lines (98 loc) · 5.46 KB
/
PA1_template.html
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
---
title: "Peer Assessment 1"
author: "ZENGYAN FAN"
date: "Sunday, February 15, 2015"
output: html_document
---
#Introduction
It is now possible to collect a large amount of data about personal movement using activity monitoring devices such as a Fitbit, Nike Fuelband, or Jawbone Up. These type of devices are part of the “quantified self” movement – a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. But these data remain under-utilized both because the raw data are hard to obtain and there is a lack of statistical methods and software for processing and interpreting the data.
This assignment makes use of data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
#Loading and preprocessing the data
##Loading the data
```{r, echo=TRUE}
data<-read.csv("C:/ZFAN/Online Course/Reproducible research/Peers Assessment 1/repdata-data-activity/activity.csv", sep=",")
```
##Process/transform the data into a format suitable for my analysis
```{r, echo=TRUE}
names(data)
steps<-data$steps
date<-data$date
interval<-data$interval
```
#What is mean total number of steps taken per day?
##Calculate the total number of steps taken per day?
```{r, echo=TRUE}
filter <- !is.na(steps)
filter_steps <- steps[filter]
filter_date <- date[filter]
days_factor <- factor(filter_date)
total_steps <- tapply(filter_steps, days_factor, FUN = sum)
print(total_steps)
```
##If you do not understand the difference between a histogram and a barplot, research the difference between them. Make a histogram of the total number of steps taken each day.
```{r, echo=TRUE}
hist(total_steps,xlab = "Total number of steps per day",
main = "Distribution of total steps per day")
```
##Calculate and report the mean and median of the total number of steps taken per day.
```{r, echo=TRUE}
mean_steps<-round(mean(total_steps))
median_steps<-round(median(total_steps))
print(mean_steps)
print(median_steps)
```
# What is the average daily activity pattern?
##Make a time series plot (i.e. type = "l") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r, echo=TRUE}
mn_int <- tapply(steps, interval, mean, na.rm=T)
plot(mn_int ~ unique(interval), type="l", xlab = "5-min interval",ylab="the average number of steps")
```
##Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r, echo=TRUE}
print(mn_int[which.max(mn_int)])
```
#Imputing missing values
##Calculate and report the total number of missing values in the dataset
```{r, echo=TRUE}
table(is.na(data) == TRUE)
```
##Devise a strategy for filling in all of the missing values in the dataset.
I use the mean for that 5-minute interval for filling in all of the missing values in the dataset.
##Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r, echo=TRUE}
data2<-data
for (i in 1:nrow(data2)){
if(is.na(data$steps[i])){
data2$steps[i]<- mn_int[[as.character(data[i, "interval"])]]
}
}
```
##Make a histogram of the total number of steps taken each day and Calculate and report the mean and median total number of steps taken per day. Do these values differ from the estimates from the first part of the assignment? What is the impact of imputing missing data on the estimates of the total daily number of steps?
```{r, echo=TRUE}
stepsperday2 <- tapply(data2$steps, data2$date, sum, na.rm=T)
hist(stepsperday2, xlab = "sum of steps per day", main = "histogram of steps per day")
mean2<-round(mean(stepsperday2))
median2<-round(median(stepsperday2))
print(mean2)
print(median2)
summary<-NULL
summary <- data.frame(mean = c(mean_steps, mean2), median = c(median_steps, median2),
row.names =c("with NA's", "without NA's"))
print(summary)
```
#Are there differences in activity patterns between weekdays and weekends?
##Create a new factor variable in the dataset with two levels – “weekday” and “weekend” indicating whether a given date is a weekday or weekend day.
```{r, echo=TRUE}
data2$weekday <- c("weekday")
data2[weekdays(as.Date(data2$date)) %in% c("Saturday", "Sunday", "samedi", "dimanche", "saturday", "sunday", "Samedi", "Dimanche"), ][4] <- c("weekend")
table(data2$weekday == "weekend")
data2$weekday <- factor(data2$weekday)
```
##Make a panel plot containing a time series plot (i.e. type = "l") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all weekday days or weekend days (y-axis). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
```{r, echo=TRUE}
data2_weekend <- subset(data2, data2$weekday == "weekend")
data2_weekday <- subset(data2, data2$weekday == "weekday")
mean_data2_weekday <- tapply(data2_weekday$steps, data2_weekday$interval, mean, na.rm=T)
mean_data2_weekend <- tapply(data2_weekend$steps, data2_weekend$interval, mean, na.rm=T)
plot(mean_data2_weekday ~ unique(data2_weekday$interval), type="l", xlab = "5-min interval",ylab="the average number of steps",main="weekday")
plot(mean_data2_weekend ~ unique(data2_weekend$interval), type="l", xlab = "5-min interval",ylab="the average number of steps",main="weekend")