========================================================
unzip("activity.zip")
data <- read.csv("activity.csv")
- Make a histogram of the total number of steps taken each day
steps.perday <- aggregate(steps ~ date, data, FUN = sum)
barplot(steps.perday$steps, names.arg = steps.perday$date, main = "Total Number of Steps Per Day ",
xlab = "Date", ylab = "Steps", col = "blue")
- Calculate and report the mean and median total number of steps taken per day
mean(steps.perday$steps)
## [1] 10766
median(steps.perday$steps)
## [1] 10765
- 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)
steps.interval <- aggregate(steps ~ interval, data, FUN = mean)
plot(steps.interval, type = "l", main = "Average Daily Activity Pattern", xlab = "5-minute Intervals Over Day",
ylab = "Average Steps Taken Over All Days")
- Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
steps.interval$interval[which.max(steps.interval$steps)]
## [1] 835
- Calculate and report the total number of missing values in the
dataset (i.e. the total number of rows with
NA
s)
sum(is.na(data))
## [1] 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.
In my opinion,I will use the means for the 5-minute intervals as fillers for missing values.
-
Create a new dataset that is equal to the original dataset but with the missing data filled in.
imputed.data <- merge(data, steps.interval, by = "interval", suffixes = c("",
".y"))
nas <- is.na(imputed.data$steps)
imputed.data$steps[nas] <- imputed.data$steps.y[nas]
imputed.data <- imputed.data[, c(1:3)]
- 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?
imputed.steps.per.day <- aggregate(steps ~ date, data = imputed.data, FUN = sum)
barplot(imputed.steps.per.day$steps, names.arg = imputed.steps.per.day$date,
main = "Total Number of Steps Per Day With Imputed Values", xlab = "Date",
ylab = "Steps", col = "red")
mean(imputed.steps.per.day$steps)
## [1] 10766
median(imputed.steps.per.day$steps)
## [1] 10766
After imputing the missing step values, the mean total number of steps per day remained unchanged while the median total number of steps per day changed slightly.The impact of the imputation was a slight increase in the median total number of steps per day.
- 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.
imputed.data$date <- as.Date(imputed.data$date)
weekend.days <- c("Saturday", "Sunday")
imputed.data$daytype <- as.factor(sapply(imputed.data$date, function(x) ifelse(weekdays(x) %in%
weekend.days, "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).
par(mfrow = c(2, 1))
for (type in c("weekend", "weekday")) {
steps.type <- aggregate(steps ~ interval, data = imputed.data, subset = imputed.data$daytype ==
type, FUN = mean)
plot(steps.type, type = "l", main = type)
}