forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPA1_template.Rmd
142 lines (90 loc) · 4.45 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
## Loading and preprocessing the data
Unzip data to obtain a csv file.
```{r}
library(data.table)
library(tidyverse)
unzip("activity.zip")
```
## Reading csv data into Data.table
```{r}
d <- fread('activity.csv')
d$date <- as.Date(d$date)
```
## What is mean total number of steps taken per day?
1. Calculate the total number of steps taken per day
```{r}
sl <- d[, c(lapply(.SD, sum, na.rm = FALSE)), .SDcols = c("steps"), by = .(date)]
```
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.
```{r}
ggplot(sl, aes(x = steps)) +
geom_histogram(fill = "blue", binwidth = 1000)
```
3. Calculate and report the mean and median of the total number of steps taken per day
```{r}
(DailyStepsMean <- mean(sl$steps, na.rm = TRUE))
(DailyStepsMeadian <- median(sl$steps, na.rm = TRUE))
```
## What is the average daily activity pattern?
1. Make a time series plot (i.e. 𝚝𝚢𝚙𝚎 = "𝚕") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r}
int <- d[, c(lapply(.SD, mean, na.rm = TRUE)), .SDcols = c("steps"), by = .(interval)]
ggplot(int, aes(x = interval, y = steps)) +
geom_line()
```
2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
int[which.max(int$steps), 1]
```
## Imputing missing values
1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with 𝙽𝙰s)
```{r}
d[is.na(steps), .N ]
# or
sum(is.na(d$steps))
```
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.
```{r}
# Using median for filling missing values of dataset.
d[is.na(steps), "steps"] <- d[, c(lapply(.SD, median, na.rm = TRUE)), .SDcols = c("steps")]
```
3. Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
data.table::fwrite(x = d, file = "tidyData.csv", quote = FALSE)
```
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?
```{r}
# Number of steps per day
totalSteps <- d[, c(lapply(.SD, sum)), .SDcols = c("steps"), by = .(date)]
# mean
mean(totalSteps$steps)
# median
median(totalSteps$steps)
```
```{r}
# histogram of the total number of steps taken each day
ggplot(totalSteps, aes(x = steps)) + geom_histogram(fill = "blue", binwidth = 1000)
```
These values differ from the estimates from the first part of the assignment:
First part (with NAs) | Second part (filling NAs with median)
--- | ---
mean = 10766.19 | mean = 10766.19
median = 10765 | median = 10395
## Are there differences in activity patterns between weekdays and weekends?
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.
```{r}
Sys.setlocale("LC_TIME", "C") # change language: From Portuguese (Brazil) to English.
d[, date := as.POSIXct(date, format = "%Y-%m-%d")]
d[, `Day of Week`:= weekdays(x = date)]
d[grepl(pattern = "Monday|Tuesday|Wednesday|Thursday|Friday", x = `Day of Week`), "weekday or weekend"] <- "weekday"
d[grepl(pattern = "Saturday|Sunday", x = `Day of Week`), "weekday or weekend"] <- "weekend"
d[, `weekday or weekend` := as.factor(`weekday or weekend`)]
```
2. Make a panel plot containing a time series plot (i.e. 𝚝𝚢𝚙𝚎 = "𝚕") 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}
d[is.na(steps), "steps"] <- d[, c(lapply(.SD, median, na.rm = TRUE)),
.SDcols = c("steps")]
int <- d[, c(lapply(.SD, mean, na.rm = TRUE)), .SDcols = c("steps"),
by = .(interval, "weekday or weekend")]
ggplot(d , aes(x = interval , y = steps, color='weekday or weekend')) + geom_line() + facet_wrap(~'weekday or weekend' , ncol = 1, nrow=2)
```