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
106 lines (83 loc) · 4.03 KB
/
Copy pathPA1_template.Rmd
File metadata and controls
106 lines (83 loc) · 4.03 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
# Reproducible Research: Peer Assessment 1
## Loading and preprocessing the data
Extract the original activity.zip file, to get activity.csv
```{r echo=TRUE}
unzip("activity.zip")
```
Load the data
```{r echo=TRUE}
data <- read.csv("activity.csv", na.string="NA", colClasses=c("integer", "Date", "integer"))
```
Process/transform the data
```{r echo=TRUE}
data$date <- as.Date(data$date, format = "%Y-%m-%d")
data$interval <- formatC(data$interval, width = 4, format = "d", flag = "0")
datetime <- strptime(paste(data$date,data$interval), "%F %H%M")
data <- cbind(data,datetime)
```
## What is mean total number of steps taken per day?
Histogram of the total number of steps taken each day
```{r echo=TRUE}
data_steps_sum <- with(data, aggregate(list(Sum_Steps = steps), by=list(Date = date), FUN=sum))
plot(data_steps_sum, type = "h", main = "Total Steps Taken Each Day", xlab = "Date", ylab = "Total Steps")
```
Mean total number of steps taken per Day
```{r echo=TRUE}
data_steps_mean <- with(data, aggregate(list(Mean_Steps = steps), by=list(Date = date), FUN=mean))
print(data_steps_mean)
```
Median total number of steps taken per Day
```{r echo=TRUE}
data_steps_median <- with(data, aggregate(list(Median_Steps = steps), by=list(Date = date), FUN=median, na.rm=TRUE))
print(data_steps_median)
```
## What is the average daily activity pattern?
Time series plot of the 5-minute interval and the average number of steps taken, averaged across all days
```{r echo=TRUE}
data_interval_mean <- with(data, aggregate(list(Mean_Steps = steps), by=list(Interval = interval), FUN=mean, na.rm=TRUE))
data_interval_mean_plot <- with(data_interval_mean, plot(Interval, Mean_Steps, type="l"))
```
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r echo=TRUE}
data[order(data_interval_mean$Mean_Steps, decreasing=TRUE)[1],]$interval
```
## Imputing missing values
Total number of missing values in the dataset
```{r echo=TRUE}
missing_steps = matrix(is.na(data$steps))
missing_steps_count = sum(missing_steps)
```
New dataset that is equal to the original dataset but with the missing data filled in
```{r echo=TRUE}
new_data = data
new_data[missing_steps,]$steps = data_interval_mean$Mean_Steps
```
Histogram of the total number of steps taken each day
```{r echo=TRUE}
new_data_steps_sum <- with(new_data, aggregate(list(Sum_Steps = steps), by=list(Date = date), FUN=sum))
plot(new_data_steps_sum, type = "h", main = "Total Steps Taken Each Day - after missing values were imputed", xlab = "Date", ylab = "Total Steps")
```
New mean total number of steps taken per Day
```{r echo=TRUE}
new_data_steps_mean <- with(new_data, aggregate(list(Mean_Steps = steps), by=list(Date = date), FUN=mean, na.rm=TRUE))
print(new_data_steps_mean)
```
New median total number of steps taken per Day
```{r echo=TRUE}
new_data_steps_median <- with(new_data, aggregate(list(Median_Steps = steps), by=list(Date = date), FUN=median, na.rm=TRUE))
print(new_data_steps_median)
```
## Are there differences in activity patterns between weekdays and weekends?
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}
day_type <- factor(weekdays(new_data$date) %in% c("Saturday", "Sunday"), labels = c("weekday", "weekend"))
new_data = cbind(new_data, day_type)
```
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)
```{r}
new_data_interval_mean_weekday <- with(new_data[new_data$day_type=="weekday",], aggregate(list(Mean_Steps = steps), by=list(Interval = interval), FUN=mean, na.rm=TRUE))
new_data_interval_mean_weekend <- with(new_data[new_data$day_type=="weekend",], aggregate(list(Mean_Steps = steps), by=list(Interval = interval), FUN=mean, na.rm=TRUE))
plot(new_data_interval_mean_weekday, type="l", col="red")
lines(new_data_interval_mean_weekend, col="green")
legend(x = 235, legend="red - weekday, green - weekend")
```