Loading and preprocessing the data

data.df <- read.csv(unz('activity.zip', 'activity.csv'), header=TRUE)

What is mean total number of steps taken per day?

  1. Calculate the total number of steps taken per day
library(dplyr)
## 
## Attaching package: 'dplyr'
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data.df.tbl <- tbl_df(data.df)
data.per.date <- data.df.tbl %>% filter(steps >= 0) %>% 
    group_by(date) %>% summarize(total = sum(steps), avg = mean(steps))
  1. Make a histogram of the total number of steps taken each day
hist(data.per.date$total, main = "Total number of steps taken each day")

  1. Calculate and report the mean and median of the total number of steps taken per day
date.mean <- mean(data.per.date$total)
date.median <- median(data.per.date$total)

print (date.mean)
## [1] 10766.19
print (date.median)
## [1] 10765

What is the average daily activity pattern?

  1. 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)
data.per.interval <- data.df.tbl %>% 
    filter(steps >= 0) %>% 
    group_by(interval) %>% 
    summarize(total = sum(steps), avg = mean(steps))

with(data.per.interval
     , plot(x = interval, y = avg, type="l", xlab = "Interval", ylab = "Average number of stpes"))

  1. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
avg.steps.max <- data.per.interval[which.max(data.per.interval$avg),]
print (avg.steps.max)
## Source: local data frame [1 x 3]
## 
##   interval total      avg
## 1      835 10927 206.1698

It has a maximum value of 206.1698113 on 835 5-minute interval.

Imputing missing values

  1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
sum(is.na(data.df$steps))
## [1] 2304
sum(is.na(data.df$date))
## [1] 0
sum(is.na(data.df$interval))
## [1] 0
steps.na.count <- sum(is.na(data.df$steps))

So, there is no missing value on date and interval vairables. The total number of missing values on stpes is 2304.

  1. 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.

I will use the mean for that 5-minute interval for filling in all of the missing values in the dataset.

  1. Create a new dataset that is equal to the original dataset but with the missing data filled in.
data.df.tbl2 <- data.df.tbl
steps.na <- is.na(data.df.tbl2$steps)
data.df.tbl2[steps.na, 1] <- with(data.per.interval, data.per.interval[interval == data.df.tbl2[steps.na, 3]]$avg)
  1. 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?
data.per.date2 <- data.df.tbl2 %>% 
    filter(steps >= 0) %>% 
    group_by(date) %>% 
    summarize(total = sum(steps), avg = mean(steps))

hist(data.per.date2$total, main = "Total number of steps with imputing NA")

date2.mean <- mean(data.per.date2$total)
date2.median <- median(data.per.date2$total)

print (date2.mean)
## [1] 10766.19
print (date2.median)
## [1] 10766.19

Are there differences in activity patterns between weekdays and weekends?

  1. 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.
data.df.tbl2$week_day <- weekdays(as.Date(data.df.tbl2$date))
data.df.tbl2$week_type <- with(data.df.tbl2, ifelse(week_day == "Sunday" | week_day == "Saturday"
                                                    , "weekend", "weekday"))
  1. 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). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
data.per.weektype <- data.df.tbl2 %>% 
    filter(steps >= 0) %>% 
    group_by(week_type, interval) %>% 
    summarize(total = sum(steps), avg = mean(steps))

library(lattice)
xyplot(avg ~ interval | week_type, type="l", xlab="Interval", ylab="Average number of steps"
       , data = data.per.weektype, layout = c(1, 2))