Show any code that is needed to
read.csv()
).# load the data
activity <- read.csv("activity.csv")
# check the data
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
str(activity)
## 'data.frame': 17568 obs. of 3 variables:
## $ steps : int NA NA NA NA NA NA NA NA NA NA ...
## $ date : Factor w/ 61 levels "2012-10-01","2012-10-02",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ interval: int 0 5 10 15 20 25 30 35 40 45 ...
# change date to calss factor to class Date
activity$date <- as.Date(activity$date, format = "%Y-%m-%d")
For this part of the assignment, you can ignore the missing values in the dataset.
# the total number of steps taken per day is stored in the variable called "total_step"
total_step <- aggregate(steps ~ date, data = activity, sum, na.rm = TRUE)
head(total_step)
## 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
par(mfrow = c(1, 1))
# use base plotting system and more bins than the default setting
hist(total_step$steps, breaks = 20,
main = "Total Number of Steps Taken Each Day",
col = "grey", border = "white", xlab = "Step", axes = FALSE)
axis(1)
axis(2, las = 1)
mean(total_step$steps)
## [1] 10766.19
median(total_step$steps)
## [1] 10765
type = "l"
) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis).avg_step <- aggregate(steps ~ interval, data = activity, mean, na.rm = TRUE)
plot(avg_step$interval, avg_step$steps, type = "l", lwd = 2, col = "navy",
main = "Time Series: Average Number of Steps Taken", axes = FALSE,
xlab = "5-minute interval", ylab = "Average number of steps")
axis(1)
axis(2, las = 1)
avg_step$interval[which.max(avg_step$steps)]
## [1] 835
The 835-th 5-minute interval contains the maximum number of steps.
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.
NA
s).sum(is.na(activity)) # or dim(activity[activity$steps == "NA", ])[1]
## [1] 2304
There are 2304 missing values in the dataset.
Here I use the mean of 5-minute interval to fill in the values of the missing values.
imp <- activity # new dataset called imp
for (i in avg_step$interval) {
imp[imp$interval == i & is.na(imp$steps), ]$steps <-
avg_step$steps[avg_step$interval == i]
}
head(imp) # no NAs
## 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
sum(is.na(imp)) # should be 0
## [1] 0
total_step_imp <- aggregate(steps ~ date, data = imp, sum, na.rm = TRUE)
hist(total_step_imp$steps, breaks = 20,
main = "Total Number of Steps Taken Each Day (Imputed)",
col = "grey", border = "white", xlab = "Step", axes = FALSE)
axis(1)
axis(2, las = 1)
mean(total_step_imp$steps)
## [1] 10766.19
median(total_step_imp$steps)
## [1] 10766.19
The mean is the same as the mean from the first part of the assignment, but the median is not, although their values are close. Imputing missing data using the average of the 5-minute interval results in more data points equal to the mean and smaller variation of the distribution. Since many data points have the same values as the mean, the median is much likely to be the same as the mean as well.
For this part the weekdays()
function may be of some help here. Use the dataset with the filled-in missing values for this part.
imp$day <- weekdays(imp$date)
imp$week <- ""
imp[imp$day == "Saturday" | imp$day == "Sunday", ]$week <- "weekend"
imp[!(imp$day == "Saturday" | imp$day == "Sunday"), ]$week <- "weekday"
imp$week <- factor(imp$week)
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.avg_step_imp <- aggregate(steps ~ interval + week, data = imp, mean)
library(lattice)
xyplot(steps ~ interval | week, data = avg_step_imp, type = "l", lwd = 2,
layout = c(1, 2),
xlab = "5-minute interval",
ylab = "Average number of steps",
main = "Average Number of Steps Taken (across all weekday days or weekend days)")