This is an R Markdown document for the Nike+ running app analysis.

library(lattice)
library(knitr)
library(ggplot2)
active <- read.csv("activity.csv", header=TRUE, sep=",",stringsAsFactors = FALSE, colClasses = c("numeric","Date","numeric"))

l <- aggregate(steps~date, active, sum)
ggplot(data = l, aes(x = steps)) + geom_histogram(binwidth = 1000, color = "red", fill = "green") + labs(title="Histogram of Steps Taken per Day",x = "Number of Steps per Day", y = "Number of times in a day(Count)") 

steps_mean <- mean(l$steps, na.rm = TRUE)
print(steps_mean)
## [1] 10766.19
steps_median <- median(l$steps, na.rm = TRUE)
print(steps_median)
## [1] 10765
time_series <- tapply(active$steps, active$interval, mean, na.rm = "TRUE")
plot(row.names(time_series), time_series, type = "l", xlab = "Interval of 5 minutes", ylab = "Average of all days", main = "Average no. of steps taken", col = "red")

max_interval <- which.max(time_series)
names(max_interval)
## [1] "835"

Replacing NA values with mean of the number of steps

active$steps[which(is.na(active$steps))] <- mean(active$steps, na.rm = TRUE)

The total no. of steps taken each day

StepsTotal2 <- aggregate(steps ~ date, data = active, sum, na.rm = TRUE)

ggplot(data = StepsTotal2, aes(x = steps)) + geom_histogram(binwidth = 1000, color = "red", fill = "yellow") + labs(title="Histogram of Total steps by day",x = "Day", y = "Number of times in a day(Count)") 

And the mean and median is

mean(StepsTotal2$steps)
## [1] 10766.19
median(StepsTotal2$steps)
## [1] 10766.19

Finding Relation betwwen activity patterns in Weekdays and Weekends

day <- weekdays(active$date)
daylevel <- vector()
for (i in 1:nrow(active)) {
    if (day[i] == "Saturday") {
        daylevel[i] <- "Weekend"
    } else if (day[i] == "Sunday") {
        daylevel[i] <- "Weekend"
    } else {
        daylevel[i] <- "Weekday"
    }
}
active$daylevel <- daylevel
active$daylevel <- factor(active$daylevel)

stepseachday <- aggregate(steps ~ interval + daylevel, data = active, mean)
names(stepseachday) <- c("interval", "daylevel", "steps")


xyplot(steps ~ interval | daylevel, stepseachday, type = "l", layout = c(1, 2), 
    xlab = "Interval", ylab = "Number of steps")