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.
temp <- tempfile()
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip",temp)
activity <- read.csv(unz(temp, "activity.csv"), header = TRUE,colClasses = c("numeric", "Date", "numeric"))
unlink(temp)
head(activity)
## steps date interval
## 1 NA 2012-10-01 0
## 2 NA 2012-10-01 5
## 3 NA 2012-10-01 10
## 4 NA 2012-10-01 15
## 5 NA 2012-10-01 20
## 6 NA 2012-10-01 25
activity_NONA <- na.omit(activity)
head(activity_NONA)
## steps date interval
## 289 0 2012-10-02 0
## 290 0 2012-10-02 5
## 291 0 2012-10-02 10
## 292 0 2012-10-02 15
## 293 0 2012-10-02 20
## 294 0 2012-10-02 25
Calculate the total number of steps taken per day
library(ggplot2)
activity_per_day <- aggregate(steps~date,activity_NONA, sum)
head(activity_per_day)
## date steps
## 1 2012-10-02 126
## 2 2012-10-03 11352
## 3 2012-10-04 12116
## 4 2012-10-05 13294
## 5 2012-10-06 15420
## 6 2012-10-07 11015
ggplot(data = activity_per_day) +
geom_histogram(aes(steps),binwidth = 500, fill = 'steelblue3', col = 'black') +
ggtitle("Total Number of Steps Taken Each Day")
Calculate and report the mean and median of the total number of steps taken per day
summary(activity_per_day)
## date steps
## Min. :2012-10-02 Min. : 41
## 1st Qu.:2012-10-16 1st Qu.: 8841
## Median :2012-10-29 Median :10765
## Mean :2012-10-30 Mean :10766
## 3rd Qu.:2012-11-16 3rd Qu.:13294
## Max. :2012-11-29 Max. :21194
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)
activity_per_interval <- aggregate(steps~interval, activity_NONA, mean)
ggplot(data = activity_per_interval) +
geom_line(aes(interval,steps)) +
ggtitle("Average Number of Steps Taken per Interval")
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
activity_per_interval[which.max(activity_per_interval$steps),]
## interval steps
## 104 835 206.1698
Note that there are a number of days/intervals where there are missing values. The presence of missing days may introduce bias into some calculations or summaries of the data.
Calculate and report the total number of missing values in the dataset.
colSums(is.na(activity))
## steps date interval
## 2304 0 0
Create a new dataset that is equal to the original dataset but with the missing data filled in.
activity_filled <- activity
activity_filled$steps <- ifelse(is.na(activity_filled$steps) == TRUE, activity_per_interval$steps[activity_per_interval$interval %in% activity_filled$interval], activity_filled$steps)
head(activity_filled)
## steps date interval
## 1 1.7169811 2012-10-01 0
## 2 0.3396226 2012-10-01 5
## 3 0.1320755 2012-10-01 10
## 4 0.1509434 2012-10-01 15
## 5 0.0754717 2012-10-01 20
## 6 2.0943396 2012-10-01 25
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?
activity_filled2 <- aggregate(steps~date, activity_filled,sum)
ggplot(data = activity_filled2) +
geom_histogram(aes(steps), fill = 'red', col = 'black') +
ggtitle("Total Number of Steps Taken Each Day - Missing Values Added")
summary(activity_filled2)
## date steps
## Min. :2012-10-01 Min. : 41
## 1st Qu.:2012-10-16 1st Qu.: 9819
## Median :2012-10-31 Median :10766
## Mean :2012-10-31 Mean :10766
## 3rd Qu.:2012-11-15 3rd Qu.:12811
## Max. :2012-11-30 Max. :21194
It was expected to see a min difference since we were replacing NA values with the average values. The mean and median are almost the same (median higher by 1 step), only the frequency on the histogram is not the highest on 10,000 steps but the next one.
For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part.
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.
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.
activity_filled$weekdayType <- ifelse(weekdays(activity_filled$date) %in% c("Saturday", "Sunday"),
"weekend", "weekday")
library(tidyverse)
activity_filled3 <- activity_filled %>% group_by(interval, weekdayType) %>% summarise(meanSteps = mean(steps))
head(activity_filled3)
## # A tibble: 6 x 3
## # Groups: interval [3]
## interval weekdayType meanSteps
## <dbl> <chr> <dbl>
## 1 0 weekday 2.25
## 2 0 weekend 0.215
## 3 5 weekday 0.445
## 4 5 weekend 0.0425
## 5 10 weekday 0.173
## 6 10 weekend 0.0165
ggplot(data = activity_filled3) + geom_line(aes(interval,meanSteps)) + facet_grid(weekdayType~.)
There is a difference in paterns between weekdays and weekends. The peak over weekdays seem to be in the morning while over weekends there is more balanced activity through whole day.