Show any code that is needed to
if(!file.exists("activity.csv")){
unzip("activity.zip")
}
data<- read.csv("activity.csv")
data$date<- as.Date(data$date)
For this part of the assignment, you can ignore the missing values in the dataset.
stepsbyday<- tapply(data$steps, data$date, sum, na.rm=TRUE)
library(ggplot2)
qplot(stepsbyday, xlab="No. of Steps Taken Each Day", ylab="Total Frequency", binwidth=500)
medianbyday<- median(stepsbyday)
meanbyday<- mean(stepsbyday)
type = "l"
) of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)avg<- tapply(data$steps, data$interval, mean, na.rm=TRUE)
plot(names(avg), avg, xlab="5-min interval", type="l", ylab="Average no. of steps")
maxavg<- max(avg)
maxinterval<- as.numeric(names(avg)[which(avg==max(avg))])
Note that there are a number of days/intervals where there are missing values (coded as NA). The presence of missing days may introduce bias into some calculations or summaries of the data.
totalna<- sum(is.na(data$steps))
# creating a copy of data set so that the missing value can be imputed in it
imputedata<- data
# Devise a strategy for filling in all of the missing values in the datase.
# In place of NA, using the mean for that 5-minute interval
imputedata$steps[which(is.na(data$steps))]<- as.vector(avg[as.character(data[which(is.na(data$steps)),3])])
stepseachday<- tapply(imputedata$steps, imputedata$date, sum, na.rm=TRUE)
qplot(stepseachday, xlab="No. of Steps Taken Each Day", ylab="Total Frequency", binwidth=500)
medianEachDayImputed<- median(stepseachday)
meanEachDayImputed<- mean(stepseachday)
For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part.
imputedata$dayType<- ifelse(as.POSIXlt(imputedata$date)$wday %in% c(0,6), "weekends","weekdays")
aggregateData<- aggregate(steps ~ interval + dayType, data=imputedata, mean)
ggplot(aggregateData, aes(interval, steps)) +
geom_line() +
facet_grid(dayType ~ .) +
xlab("5-minute interval") +
ylab("avarage number of steps")