unzip("activity.zip")
## Warning in unzip("activity.zip"): error 1 in extracting from zip file
activity <- read.csv("activity.csv")
steps.date <- aggregate(steps ~ date, data=activity, FUN=sum)
barplot(steps.date$steps, names.arg=steps.date$date, xlab="date", ylab="steps")
mean(steps.date$steps)
## [1] 10766.19
median(steps.date$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)steps.interval <- aggregate(steps ~ interval, data=activity, FUN=mean)
plot(steps.interval, type="l")
steps.interval$interval[which.max(steps.interval$steps)]
## [1] 835
NAs)sum(is.na(activity))
## [1] 2304
We will use the means for the 5-minute intervals as fillers for missing values.
activity <- merge(activity, steps.interval, by="interval", suffixes=c("",".y"))
nas <- is.na(activity$steps)
activity$steps[nas] <- activity$steps.y[nas]
activity <- activity[,c(1:3)]
steps.date <- aggregate(steps ~ date, data=activity, FUN=sum)
barplot(steps.date$steps, names.arg=steps.date$date, xlab="date", ylab="steps")
mean(steps.date$steps)
## [1] 10766.19
median(steps.date$steps)
## [1] 10766.19
The impact of the missing data seems rather low, at least when estimating the total number of steps per day.
daytype <- function(date) {
if (weekdays(as.Date(date)) %in% c("Saturday", "Sunday")) {
"weekend"
} else {
"weekday"
}
}
activity$daytype <- as.factor(sapply(activity$date, daytype))
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=activity,
subset=activity$daytype==type,
FUN=mean)
plot(steps.type, type="l", main=type)
}