-
Notifications
You must be signed in to change notification settings - Fork 37.1k
/
Copy pathPA1_template.Rmd
149 lines (109 loc) · 4.44 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
142
143
144
145
146
147
148
149
---
title: "Reproducible Research Project 1"
author: "Dyutit Mohanty"
date: "2024-03-11"
output: html_document
---
# Loading and preprocessing the data
```{r}
filename <- "activity.csv"
data <- read.csv(filename, header=TRUE)
head(data)
```
# What is mean total number of steps taken per day?
### Calculate the total number of steps taken per day
```{r}
data2 <- data[!(is.na(data$steps)), ]
steps_per_day <- aggregate(steps ~ date, data=data2, FUN = sum)
```
### Histogram of the total number of steps taken each day
```{r}
hist(steps_per_day$steps, main="Histogram of the total number of steps taken each day", xlab="Steps", ylab="Frequency")
```
### Calculate and report the mean and median of the total number of steps taken per day
```{r}
mean_steps_per_day <- mean(steps_per_day$steps)
median_steps_per_day <- median(steps_per_day$steps)
mean_steps_per_day
median_steps_per_day
```
# What is the average daily activity pattern?
### Make a time series plot of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)
```{r}
my_mean <- function(x) mean(x, na.rm = TRUE) # R kept throwing an error when "mean" was used as arg so I defined my own mean function
data3 <- aggregate(steps ~ interval, data, FUN=my_mean)
head(data3)
```
```{r}
plot(y=data3$steps,x=data3$interval, type="l", xlab="Intervals", ylab="Steps")
```
### Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
```{r}
index_max_steps <- which.max(data3$steps)
interval_max_steps <- data3$interval[index_max_steps]
interval_max_steps
max(data3$steps)
```
# Imputing missing values
### Calculate and report the total number of missing values in the dataset (total number of rows with NAs)
```{r}
data4 <- subset(data, is.na(data$steps))
dims <- dim(data4)
nrows_na <- dims[1]
nrows_na
```
### 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}
# I am using the mean for the interval
data_nona <- data
for (i in 1:nrow(data_nona)){
if (is.na(data_nona$steps[i])){
interval <- data_nona$interval[i]
index <- which(data3$interval == interval)
mean_val <- data3$steps[index]
data_nona$steps[i] <- mean_val
}
}
head(data_nona)
```
### Create a new dataset that is equal to the original dataset but with the missing data filled in.
```{r}
head(data_nona)
```
### 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}
data4 <- aggregate(steps ~ date, data_nona, FUN=sum)
hist(data4$steps, main="Histogram of the total number of steps taken each day", xlab="Steps", ylab="Frequency")
mean_steps <- mean(data4$steps)
median_steps <- median(data4$steps)
mean_steps
median_steps
```
### Slight increase in average
# 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}
data5 <- data_nona
data5$date <- as.Date(data5$date)
data5$day <- weekdays(data5$date)
for (i in 1:nrow(data5)){
if (data5$day[i] %in% c("Saturday", "Sunday")){
data5$weekday[i] <- "weekend"
}
else {
data5$weekday[i] <- "weekday"
}
}
fct_days <- factor(data5$weekday)
summary(fct_days)
```
### Make a panel plot containing a time series plot 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}
wknd_data <- data5[data5$weekday == "weekend",]
wkday_data <- data5[data5$weekday == "weekday",]
wknd_data <- aggregate(steps ~ interval, wknd_data, mean)
wkday_data <- aggregate(steps ~ interval, wkday_data, mean)
par(mfrow=c(2, 1))
plot(wknd_data$interval, wknd_data$steps, type="l")
plot(wkday_data$interval, wkday_data$steps, type="l")
```