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.
Activity monitoring data [52K]
The dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.The variables included in this dataset are:
1. What is mean total number of steps taken per day?
For this part of the assignment, you can ignore the missing values in the dataset.
2. What is the average daily activity pattern?
3. Imputing missing values
Note that there are a number of days/intervals where there are missing values (coded as ). The presence of missing days may introduce bias into some calculations or summaries of the data.
4. Are there differences in activity patterns between weekdays and weekends?
For this part the function may be of some help here. Use the dataset with the filled-in missing values for this part.
Setup to show every code chunks
knitr::opts_chunk$set(echo = TRUE)
Look at data summary and few first lines
data=read.csv("activity.csv")
data$date=as.Date(as.character(data$date))
summary(data)
## steps date interval
## Min. : 0.00 Min. :2012-10-01 Min. : 0.0
## 1st Qu.: 0.00 1st Qu.:2012-10-16 1st Qu.: 588.8
## Median : 0.00 Median :2012-10-31 Median :1177.5
## Mean : 37.38 Mean :2012-10-31 Mean :1177.5
## 3rd Qu.: 12.00 3rd Qu.:2012-11-15 3rd Qu.:1766.2
## Max. :806.00 Max. :2012-11-30 Max. :2355.0
## NA's :2304
head(data)
## steps date interval
## 1 NA 2012-10-01 0
## 2 NA 2012-10-01 5
## 3 NA 2012-10-01 10
## 4 NA 2012-10-01 15
## 5 NA 2012-10-01 20
## 6 NA 2012-10-01 25
a. Total steps taken on each day
byStep=aggregate(data$steps,by=list(data$date),sum)
names(byStep)=c("Date","TotalSteps")
head(byStep)
## Date TotalSteps
## 1 2012-10-01 NA
## 2 2012-10-02 126
## 3 2012-10-03 11352
## 4 2012-10-04 12116
## 5 2012-10-05 13294
## 6 2012-10-06 15420
b. Histogram of the total number of steps taken each day
hist(byStep$TotalSteps,breaks = 25,col = "steelblue",border = "white",
main = "Total number of Steps per Day", xlab = "Total Steps",ylim = c(0,20))
c. Mean and median of the total number of steps taken per day
mean=round(mean(byStep$TotalSteps,na.rm=TRUE),2)
median=round(median(byStep$TotalSteps,na.rm = TRUE),2)
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)
byInterval=aggregate(na.omit(data)$steps,by=list(na.omit(data)$interval),mean)
names(byInterval)=c("Interval","Mean.Steps")
plot(x=byInterval$Interval,y=byInterval$Mean.Steps,type = "l",col =10,lwd=2,main ="Average of Steps accross all Days",xlab="Interval",ylab = "Step")
b. The 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps
byInterval[byInterval$Mean.Steps==max(byInterval$Mean.Steps),]
## Interval Mean.Steps
## 104 835 206.1698
a. Total number of missing values in the dataset (i.e. the total number of rows with NAs)
sapply(X=data,FUN = function(x) sum(is.na(x)))
## steps date interval
## 2304 0 0
b. All the NAs will be replaced by the mean of steps taken on its interval
c. New dataset that is equal to the original dataset but with the missing data filled in.
complete.data=data
for (i in c(which(is.na(data$steps)))) {
complete.data$steps[i]=byInterval[which(byInterval$Interval==data$interval[i]),2]
}
sapply(X=complete.data,FUN = function(x) sum(is.na(x)))
## steps date interval
## 0 0 0
d. Histogram of the total number of steps taken each day
byStep.new=aggregate(complete.data$steps,by=list(complete.data$date),sum)
names(byStep.new)=c("Date","TotalSteps")
hist(byStep.new$TotalSteps,breaks = 25,col = "steelblue",border = "white",
main = "Total number of Steps per Day", xlab = "Total Steps",
ylim = c(0,20))
e. Any differences in mean and median between original data and data with NAs replaced?
Compare between histogram
newmean=round(mean(byStep.new$TotalSteps),2)
newmedian=round(median(byStep.new$TotalSteps),2)
par(mfrow=c(1,2))
hist(byStep$TotalSteps,breaks = 25,col = "steelblue",border = "white",
main=NULL,xlab = "Total Steps per day before fill NAs",ylim = c(0,20))
hist(byStep.new$TotalSteps,breaks = 25,col = "steelblue",border = "white",
main=NULL,xlab = "Total Steps per day after fill NAs",
ylim = c(0,20))
a. New factor variable in the dataset with two levels - “weekday” and “weekend” indicating whether a given date is a weekday or weekend day
new.data=complete.data
new.data$day.of.week=ifelse(weekdays(as.Date(new.data$date)) %in% c("Saturday","Sunday"),
"weekend","weekday")
new.data=aggregate(new.data$steps,by=list(new.data$interval,new.data$day.of.week),mean)
names(new.data)=c("Interval","day.of.week","Mean.Steps")
head(new.data)
## Interval day.of.week Mean.Steps
## 1 0 weekday 2.25115304
## 2 5 weekday 0.44528302
## 3 10 weekday 0.17316562
## 4 15 weekday 0.19790356
## 5 20 weekday 0.09895178
## 6 25 weekday 1.59035639
b. 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)
library(ggplot2)
ggplot(new.data,aes(x=Interval,y=Mean.Steps,color=day.of.week))+
geom_line()+
facet_grid(day.of.week~.)+
labs(title="Means of Steps by Interval", x="Interval",y="Mean Steps")