## libraries
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(knitr)
# Download & unzip file for processing. # Read and load CSV file into a Data Frame.
if (!file.exists('./newData')){dir.create('./newData')}
Url <- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(Url, destfile = "./newData/activity.zip")
# unzip the file
unzip(zipfile = "./newData/activity.zip", exdir = './newData')
# Read csv file
activity <- read.csv("./newData/activity.csv")
Process/transform the data (if necessary) into a format suitable for your analysis
# Date conversion
activity$date<-ymd(activity$date)
length(unique(activity$date))
## [1] 61
Here the missing values in the dataset is ignored
Calculate the total number of steps taken per day
# total number of daily steps
dailySteps <- activity %>%
group_by(date) %>%
summarize(sumSteps = sum(steps, na.rm = TRUE))
#Display first 10 rows of data
head(dailySteps,10)
## # A tibble: 10 x 2
## date sumSteps
## <date> <int>
## 1 2012-10-01 0
## 2 2012-10-02 126
## 3 2012-10-03 11352
## 4 2012-10-04 12116
## 5 2012-10-05 13294
## 6 2012-10-06 15420
## 7 2012-10-07 11015
## 8 2012-10-08 0
## 9 2012-10-09 12811
## 10 2012-10-10 9900
Make a histogram of the total number of steps taken each day
hist(dailySteps$sumSteps, main = "Histogram of Daily Steps", xlab = 'Steps', col = 'pink')
Calculate and report the mean and median of the total number of steps taken per day
dailyMeanSteps <- round(mean(dailySteps$sumSteps),digits = 2)
dailyMedianSteps <- round(median(dailySteps$sumSteps),digits = 2)
print(paste("The mean of the total number of daily steps is: ", dailyMeanSteps))
## [1] "The mean of the total number of daily steps is: 9354.23"
print(paste("The median of the total number of daily steps is: ", dailyMedianSteps))
## [1] "The median of the total number of daily steps is: 10395"
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)
stepsPerInterval <- activity %>%
group_by(interval) %>%
summarize(dailyMeanSteps = mean(steps, na.rm = TRUE))
#Display first 10 rows of data
head(stepsPerInterval,10)
## # A tibble: 10 x 2
## interval dailyMeanSteps
## <int> <dbl>
## 1 0 1.72
## 2 5 0.340
## 3 10 0.132
## 4 15 0.151
## 5 20 0.0755
## 6 25 2.09
## 7 30 0.528
## 8 35 0.868
## 9 40 0
## 10 45 1.47
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
plot(stepsPerInterval$dailyMeanSteps ~ stepsPerInterval$interval,
col="blue", type="l", xlab = "5 Minute Intervals", ylab = "Average Number of Steps",
main = "Steps By Time Interval")
print(paste("Interval containing the most steps on average: ",stepsPerInterval$interval[which.max(stepsPerInterval$dailyMeanSteps)]))
## [1] "Interval containing the most steps on average: 835"
print(paste("Average steps for that interval: ",round(max(stepsPerInterval$dailyMeanSteps),digits=2)))
## [1] "Average steps for that interval: 206.17"
Note that there are a number of days/intervals where there are missing values (coded as NA). 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 (i.e. the total number of rows with NAs)
print(paste("The total number of rows with NA is: ",sum(is.na(activity$steps))))
## [1] "The total number of rows with NA is: 2304"
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.
Create a new dataset that is equal to the original dataset but with the missing data filled in.
# Code to describe and show a strategy for imputing missing data
activityNoNA <- activity
for (i in 1:nrow(activity)){
if(is.na(activity$steps[i])){
activityNoNA$steps[i]<- stepsPerInterval$dailyMeanSteps[activityNoNA$interval[i] == stepsPerInterval$interval]
}
}
#Display first 10 rows of data
head(activityNoNA,10)
## 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
## 7 0.5283019 2012-10-01 30
## 8 0.8679245 2012-10-01 35
## 9 0.0000000 2012-10-01 40
## 10 1.4716981 2012-10-01 45
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?
totalDailyStepsNoNA <- aggregate(steps ~ date, data=activityNoNA, sum)
hist(totalDailyStepsNoNA$steps)
meanDailyStepsNoNA <- mean(totalDailyStepsNoNA$steps)
medianDailyStepsNoNA <- median(totalDailyStepsNoNA$steps)
The mean didn’t change after the replacements of NAs, the median changed about 0.1% of the original value.
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.
activityNoNA$date <- as.Date(strptime(activityNoNA$date, format="%Y-%m-%d"))
activityNoNA$day <- weekdays(activityNoNA$date)
for (i in 1:nrow(activityNoNA)) {
if (activityNoNA[i,]$day %in% c("Saturday","Sunday")) {
activityNoNA[i,]$day<-"weekend"
}
else{
activityNoNA[i,]$day<-"weekday"
}
}
dailySteps <- aggregate(activityNoNA$steps ~ activityNoNA$interval + activityNoNA$day, activityNoNA, mean)
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).
names(dailySteps) <- c("interval", "day", "steps")
library(lattice)
xyplot(steps ~ interval | day, dailySteps, type = "l", layout = c(1, 2),
xlab = "Interval", ylab = "Number of steps")