It is now possible to collect a large amount of data about personal movement using activity monitoring devices such as a Fitbit, Nike Fuelband, or Jawbone Up. These type of devices are part of the âquantified selfâ movement â a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. But these data remain under-utilized both because the raw data are hard to obtain and there is a lack of statistical methods and software for processing and interpreting the data.
This assignment makes use of data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
fileurl<-"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
if (!file.exists(("data"))) {dir.create("./data")}
download.file(fileurl, destfile="./data/repdata%2Fdata%2Factivity.zip")
unzip("./data/repdata%2Fdata%2Factivity.zip", exdir="./data")
rawdata<-read.csv("./data/activity.csv", na.strings = "NA")
totalstep<-tapply(rawdata$steps, rawdata$date, sum, na.rm=T)
hist(totalstep,col="blue",main="Histogram of Total Steps taken per day",
xlab="Total Steps taken per day",cex.axis=1,cex.lab = 1)
totalmean<-mean(totalstep)
totalmedian<-median(totalstep)
steps_interval <- aggregate(steps ~ interval,
data = rawdata,
mean, na.rm = TRUE)
plot(steps ~ interval, data = steps_interval, type = "l",
xlab = "Time Intervals (5-minute)",
ylab = "Mean number of steps taken (all Days)",
main = "Average number of steps Taken at 5 minute Intervals",
col = "blue")
maxStepInterval <- steps_interval[which.max(steps_interval$steps),"interval"]
missing_rows <- sum(!complete.cases(rawdata))
## This function returns the mean steps for a given interval
intervalmean <- function(interval) {
steps_interval[steps_interval$interval==interval,"steps"]
}
modidata <- rawdata
flag = 0
for (i in 1:nrow(modidata)) {
if (is.na(modidata[i, "steps"])) {
modidata[i, "steps"] <- intervalmean(modidata[i, "interval"])
flag = flag +1
}
}
newtotalsteps <- aggregate(steps ~ date, data=modidata, sum)
hist(newtotalsteps$steps, col = "blue", xlab = "Total Number of Steps",
ylab = "Frequency", main = "Histogram of Total Number of Steps taken each Day")
### Calculate and report the mean and median total number of steps taken per day.
newmean <- mean(newtotalsteps$steps)
newmedian <- median(newtotalsteps$steps)
The mean changes because the missing values were not taken in to account and thus the count changes
The median value is different, since the median index is now being changed after imputing missing values.
library(forcats)
modidata$weekday <- as.factor(weekdays(as.POSIXct(modidata$date)))
modidata$weekday <- fct_collapse(modidata$weekday,
"workdays" = c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday"),
"weekends" = c("Saturday", "Sunday"))
library(lattice)
newsteps_interval <- aggregate(steps ~ interval + weekday, data=modidata, mean)
xyplot(steps ~ interval | factor(weekday), data = newsteps_interval, aspect = 1/2,
type = "l")