forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
127 lines (110 loc) · 5.4 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
---
title: "Reproducible Research: Peer Assessment 1"
output:
html_document:
keep_md: true
---
## Loading and preprocessing the data
```{r}
#1. Load the data (i.e. read.csv())
setwd("~/Documents/git/RepData_PeerAssessment1")
if (!file.exists("./activity.csv")) {
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip", "./activity.zip")
unzip("./activity.zip", overwrite = T, exdir = ".")
}
activity <- read.csv("./activity.csv", header = T, sep = ",")
#2. Process/transform the data (if necessary) into a format suitable for your analysis
activity$date <- as.Date(activity$date)
```
## What is mean total number of steps taken per day?
```{r}
#1. Calculate the total number of steps taken per day
su <- tapply(activity$steps, activity$date, sum, na.rm=T)
su
#2. 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
hist(su, xlab = "sum of steps per day", main = "histogram of steps per day")
#3. Calculate and report the mean and median of the total number of steps taken per day
mean_su <- round(mean(su))
median_su <- round(median(su))
print(c("The mean is",mean_su))
print(c("The median is",median_su))
```
## What is the average daily activity pattern?
```{r}
#1. 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)
mn_int <- tapply(activity$steps,
activity$interval,
mean,
na.rm=T)
plot(mn_int ~ unique(activity$interval),
type="l",
xlab = "5-min interval")
#2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
mn_int[which.max(mn_int)]
```
## Imputing missing values
```{r}
#1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
sum(is.na(activity) == TRUE)
#2. Devise a strategy for filling in all of the missing values in the dataset. The strategy does not need to be sophisticated. For example, you could use the mean/median for that day, or the mean for that 5-minute interval, etc.
#3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
activity2 <- activity # creation of the dataset that will have no more NAs
for (i in 1:nrow(activity)){
if(is.na(activity$steps[i])){
activity2$steps[i]<- mn_int[[as.character(activity[i, "interval"])]]
}
}
#4. 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?
su2 <- tapply(activity2$steps,
activity2$date,
sum,
na.rm=T)
hist(su2,
xlab = "sum of steps per day",
main = "histogram of steps per day")
mean_su2 <- round(mean(su2))
median_su2 <- round(median(su2))
print(c("The mean is",mean_su2))
print(c("The median is",median_su2))
df_summary <- rbind(data.frame(mean = c(mean_su, mean_su2),
median = c(median_su, median_su2)))
rownames(df_summary) <- c("with NA's", "without NA's")
print(df_summary)
```
## Are there differences in activity patterns between weekdays and weekends?
```{r}
#1. 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.
activity2$weekday <- c("weekday")
activity2[weekdays(as.Date(activity2[, 2])) %in%
c("Saturday", "Sunday", "samedi", "dimanche", "saturday",
"sunday", "Samedi", "Dimanche"), ][4] <- c("weekend")
activity2$weekday <- factor(activity2$weekday)
#2. 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.
activity2_weekend <- subset(activity2,
activity2$weekday == "weekend")
activity2_weekday <- subset(activity2,
activity2$weekday == "weekday")
mean_activity2_weekday <- tapply(activity2_weekday$steps,
activity2_weekday$interval,
mean)
mean_activity2_weekend <- tapply(activity2_weekend$steps,
activity2_weekend$interval,
mean)
library(lattice)
df_weekday <- NULL
df_weekend <- NULL
df_final <- NULL
df_weekday <- data.frame(interval = unique(activity2_weekday$interval),
avg = as.numeric(mean_activity2_weekday),
day = rep("weekday", length(mean_activity2_weekday)))
df_weekend <- data.frame(interval = unique(activity2_weekend$interval),
avg = as.numeric(mean_activity2_weekend),
day = rep("weekend",
length(mean_activity2_weekend)))
df_final <- rbind(df_weekday, df_weekend)
xyplot(avg ~ interval | day,
data = df_final,
layout = c(1, 2),
type = "l",
ylab = "Number of steps")
```