dataFile<-"activity.csv"
data<-read.table(dataFile, header=TRUE, sep=",", stringsAsFactors=FALSE, dec=".")
dailysteps<-aggregate(steps~date,data,sum)
library(ggplot2)
ggplot(dailysteps,aes(steps))+geom_histogram(binwidth=500)+scale_x_continuous(minor_breaks = seq(1,25000,500),breaks = seq(0,25000,1000))+theme(axis.text.x=element_text(angle = 90, vjust = 0.5))
mean(dailysteps$steps)
## [1] 10766.19
median(dailysteps$steps)
## [1] 10765
intervalsteps<-aggregate(steps~interval,data,mean)
with(intervalsteps,plot(interval,steps,type="l"))
subset(intervalsteps,steps==max(steps))$interval
## [1] 835
+ Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
+ 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.
+ Create a new dataset that is equal to the original dataset but with the missing data filled in.
+ 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?
First, I will calculate the total number of missing values.
sum(is.na(data$steps))
## [1] 2304
For these values I will substitute in the mean number of steps taken for that time interval.
newdata<-merge(data,intervalsteps,by="interval")
newdata$steps.x[is.na(newdata$steps.x)]<-newdata$steps.y[is.na(newdata$steps.x)]
newdailysteps<-aggregate(newdata$steps.x~newdata$date,newdata,sum)
ggplot(newdailysteps,aes(newdailysteps$`newdata$steps.x`))+geom_histogram(binwidth=500)+scale_x_continuous(minor_breaks = seq(1,25000,500),breaks = seq(0,25000,1000))+theme(axis.text.x=element_text(angle = 90, vjust = 0.5))+xlab("steps")
mean(newdailysteps$`newdata$steps.x`)
## [1] 10766.19
median(newdailysteps$`newdata$steps.x`)
## [1] 10766.19
8) Panel plot comparing the average number of steps taken per 5-minute interval across weekdays and weekends
newdata$daytype<-ifelse(weekdays(as.Date(newdata$date))%in%c("Saturday","Sunday"),"Weekend","Weekday")
wknewdata<-newdata[newdata$daytype=="Weekday",]
wdnewdata<-newdata[newdata$daytype=="Weekend",]
weekendsteps<-aggregate(steps.x~interval,wknewdata,mean)
weekdaysteps<-aggregate(steps.x~interval,wdnewdata,mean)
par(mfrow=c(2,1),pin=c(6,1.5))
with(weekdaysteps,plot(interval,steps.x,type="l",main="Weekday Steps",ylab = "steps"))
with(weekendsteps,plot(interval,steps.x,type="l",main="Weekend Steps",ylab = "steps"))