The following code loads the data from the zipped CSV file in the project directory.
The analysis did not require any further processing or transformation of the data.
setwd("/home/user/Projects/DataScience/Reproducible/RepData_PeerAssessment1")
activity <- read.csv(unz("activity.zip", "activity.csv"))
For this part of the assignment, we will ignore the missing values in the dataset. This is a histogram of the total number of steps taken each day. The mean and median total number of steps taken per day is then calculated below.
steps_per_day <- tapply(activity$steps, activity$date, sum, na.rm = T)
hist(steps_per_day)
steps_mean <- mean(steps_per_day)
steps_median <- median(steps_per_day)
The median number of steps per day is 10395.
The mean number of steps per day is 9354.
mean(steps_per_day)
## [1] 9354
median(steps_per_day)
## [1] 10395
The following is 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)
avg_steps_per_interval <- tapply(activity$steps, activity$interval, mean, na.rm = T)
plot(as.integer(names(avg_steps_per_interval)), avg_steps_per_interval, type = "l",
main = "Average Daily Activity Pattern", xlab = "5-minute interval", ylab = "average # of steps")
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
as.integer(names(which.max(avg_steps_per_interval)))
## [1] 835
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.
activity_cc <- complete.cases(activity)
nNAs <- length(activity_cc[activity_cc == "FALSE"])
The total number of missing values in the dataset (i.e. the total number of rows with NAs) is 2304.
Below is a strategy for filling in all of the missing values in the dataset. I use the mean number of steps for the 5-minute interval across all days in place of the NA values for that interval.
I have create a new dataframe that is equal to the original dataset but with the missing data filled in. I use a functional coding style to generate this result as opposed to creating iterative code. The mapply() function takes the number of steps (s) and the interval (i) and creates a replacement column in the df dataset by checking to see if each s value is NA and if, so replacing it with the value in the previously created avg_steps_per_interval array at the given interval.
df <- data.frame(activity)
df[, "steps"] <- mapply(function(s, i) ifelse(is.na(s), as.integer(round(avg_steps_per_interval[as.character(i)])),
s), df$steps, df$interval)
Below is a histogram of the total number of steps taken each day.
steps_per_day_no_NA <- tapply(df$steps, df$date, sum)
hist(steps_per_day_no_NA, main = "Histogram of Steps per Day (with Imputted Values for NA)")
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?
steps_mean_no_NA <- mean(steps_per_day_no_NA)
steps_median_no_NA <- median(steps_per_day_no_NA)
The median number of steps per day is 10762.
The mean number of steps per day is 1.0766 × 104.
median(steps_per_day_no_NA)
## [1] 10762
mean(steps_per_day_no_NA)
## [1] 10766
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.
df$daytype <- sapply(df$date, function(x) {
day <- weekdays(as.Date(x))
ifelse(day == "Sunday" || day == "Saturday", "weekend", "weekday")
})
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). The plot should look something like the following, which was creating using simulated data:
library(lattice)
daytype.factor <- factor(df$daytype, levels = c("weekend", "weekday"))
xyplot(df$steps ~ df$interval | daytype.factor, type = "l", layout = c(1, 2),
xlab = "interval", ylab = "number of steps")