URL<- "https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(URL,"./activity.zip",method="curl")
unzip("./activity.zip")
library(knitr)
## Warning: package 'knitr' was built under R version 3.3.2

Load

data<-read.csv("activity.csv",as.is=T)
act<- data[complete.cases(data),]

What is mean total number of steps taken per day?

1.Calculate the total number of steps taken per day

ag <- aggregate(steps~date,act,sum)

2.Make a histogram of the total number of steps taken each day

hist(ag$steps)

3.Calculate and report the mean and median of the total number of steps taken per day

print(mean(ag$steps))
## [1] 10766.19
print(median(ag$steps))
## [1] 10765

What is the average daily activity pattern?

1.Make a time series plot (i.e. 𝚝𝚢𝚙𝚎 = “𝚕”) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)

ave_int<-aggregate(steps~interval,act,mean)
plot(ave_int$interval,ave_int$steps,type="l")

2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?

max<-which.max(ave_int$steps)
print(paste(c("The interval"),ave_int[max,]$interval, c("contains the maximum number of steps")))
## [1] "The interval 835 contains the maximum number of steps"

Imputing missing values

1.Calculate and report the total number of missing values

miss<-data[!complete.cases(data),]
nrow(miss)
## [1] 2304

2.Devise a strategy for filling in all of the missing values

Find out NA and subtitute it with the mean of the interval

3.Create a new dataset that is equal to the original dataset but with the missing data filled in

for (i in 1:nrow(data)) {
    if(is.na(data$steps[i])) {
        x <- ave_int$steps[which(ave_int$interval == data$interval[i])]
        data$steps[i] <- x
    }
}
ag_steps<- aggregate(steps~date,data,sum)

4.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?

hist(ag_steps$steps)

print(mean(ag_steps$steps))
## [1] 10766.19
print(median(ag_steps$steps))
## [1] 10766.19

So, the mean hasn’t changed, but the median is different from before

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.

week_day <- function(date_val) {
    wd <- weekdays(as.Date(date_val, '%Y-%m-%d'))
    if  (!(wd == 'Saturday' || wd == 'Sunday')) {
        a <- 'Weekday'
    } else {
        a <- 'Weekend'
    }
    a
}

data$day_type <- as.factor(sapply(data$date, week_day))


library(ggplot2)

ag_steps2<- aggregate(steps ~ interval+day_type, data, mean)

g <- ggplot(ag_steps2, aes(interval, steps)) +
    geom_line(stat = "identity", aes(colour = day_type)) +
    theme_gray() +
    facet_grid(day_type ~ ., scales="fixed", space="fixed") +
    labs(x="Interval", y=expression("No of Steps")) +
    ggtitle("No of steps Per Interval by day type")
print(g)