setwd("~/Desktop/Coursera/ReproducibleResearch/PeerAssessment1")
library(knitr)
library(ggplot2)
downloadFile <- "data/activity.zip"
downloadURL <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
if(!file.exists("./data")) { dir.create("./data")}
if (!file.exists(downloadFile)) {
download.file(downloadURL, downloadFile, method = "curl");
unzip(downloadFile, overwrite = T, exdir = ".")
}
data <- read.csv("./data/activity.csv")
total.steps <- tapply(data$steps, data$date, FUN=sum, na.rm=TRUE)
qplot(total.steps, binwidth=1000, colour=I("blue"), fill=I("green"), xlab="Total Number of Steps Taken Each Day")
### * What is the average daily activity pattern? #### 1 Calculate and report the mean and median of the total number of steps taken per day
mean(total.steps, na.rm=TRUE)
## [1] 9354.23
median(total.steps, na.rm=TRUE)
## [1] 10395
averageSteps <- aggregate(x=list(steps=data$steps), by=list(interval=data$interval),
FUN=mean, na.rm=TRUE)
ggplot(data=averageSteps, aes(x=interval, y=steps)) +
geom_line(colour="blue", size=1.5) +
xlab("5 Minute Interval") +
ylab("Average Number of Steps Taken")
### * Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
averageSteps[which.max(averageSteps$steps),]
## interval steps
## 104 835 206.1698
missingValues <- is.na(data$steps)
table(missingValues)
## missingValues
## FALSE TRUE
## 15264 2304
fill.value <- function(steps, interval) {
filled <- NA
if (!is.na(steps))
filled <- c(steps)
else
filled <- (averageSteps[averageSteps$interval==interval, "steps"])
return(filled)
}
filled.data <- data
filled.data$steps <- mapply(fill.value, filled.data$steps, filled.data$interval)
total.steps <- tapply(filled.data$steps, filled.data$date, FUN=sum)
qplot(total.steps, binwidth=500, xlab="Total Number of Steps Taken Each Day", colour=I("blue"), fill=I("green"))
### * 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?
mean(total.steps)
## [1] 10766.19
median(total.steps)
## [1] 10766.19
weekday.or.weekend <- function(date) {
day <- weekdays(date)
if (day %in% c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"))
return("weekday")
else if (day %in% c("Saturday", "Sunday"))
return("weekend")
else
stop("invalid date")
}
filled.data$date <- as.Date(filled.data$date)
filled.data$day <- sapply(filled.data$date, FUN=weekday.or.weekend)
averages <- aggregate(steps ~ interval + day, data=filled.data, mean)
ggplot(averages, aes(interval, steps)) + geom_line() + facet_grid(day ~ .) +
xlab("5 Minute Interval") + ylab("Number of Steps") +
geom_line(colour="blue", size=1.25)