This is an R Markdown document for the 1st Assignment in Reproducible Research course at Coursera. On this assignment I will create a single R markdown document that can be processed by knitr. To answer the different requirements, it will follow the same structure than the assignment instructions.
The only exception is that I did not Fork/Clone the GitHub repository created for this assignment, but I started from scratch. However the repository contains
The data for this assignment can be downloaded from the course web site. But in fact, it has been also included in the repository.
The variables included in this dataset are:
steps: Number of steps taking in a 5-minute interval (missing values are coded as NA)
date: The date on which the measurement was taken in YYYY-MM-DD format
interval: Identifier for the 5-minute interval in which measurement was taken
Show any code that is needed to
read.csv())activity<-read.csv("activity.csv", as.is = TRUE, dec=".", na.strings = "NA")
For this part of the assignment, the missing values in the dataset are ignored.
hist_sum<-tapply(activity$steps, activity$date, sum)
hist(hist_sum, main="Histogram", xlab="Number of daily steps")
Mean
mean(hist_sum, na.rm=TRUE)
## [1] 10766.19
Median
median(hist_sum, na.rm=TRUE)
## [1] 10765
hist(hist_sum, main="Histogram", xlab="Number of daily steps")
abline(v=mean(hist_sum, na.rm=TRUE), col="red", lwd=2)
legend("topright", legend = c("mean"), col=c("red"), lty=c(1))
type = "l") of the 5-minute interval (x-axis) and the average number of steps taken, averaged across all days (y-axis)library(dplyr)
a_by_interval<-activity %>%
group_by(interval) %>%
summarise(avg_steps=mean(steps, na.rm=T))
with(a_by_interval, plot(avg_steps ~ interval, type="l"))
The average maximum number of steps is:
max(a_by_interval$avg_steps)
## [1] 206.1698
And it is achieved at interval:
pos_max_interval<-which.max(a_by_interval$avg_steps)
a_by_interval[pos_max_interval,"interval"][[1]]
## [1] 835
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.
NAs)bad<-is.na(activity$steps)
sum(bad)
## [1] 2304
I decided to assign the mean for that 5-minute interval to each NA value.
new_activity<-merge(activity,a_by_interval, by="interval")
new_activity<-mutate(new_activity,
steps=coalesce(steps,as.integer(round(avg_steps))))
New Mean and Median are very similar to the previous one
new_hist_sum<-tapply(new_activity$steps, new_activity$date, sum)
mean(new_hist_sum, na.rm=TRUE)
## [1] 10765.64
median(new_hist_sum, na.rm=TRUE)
## [1] 10762
And the main impact on the Histogram is that the average interval increases. As it can be seen compared to the previous histogram.
par(mfrow=c(1,2))
hist(new_hist_sum, main="New Histogram", xlab="Number of daily steps")
abline(v=mean(new_hist_sum, na.rm=TRUE), col="red", lwd=2)
hist(hist_sum, ylim=c(0,35), main="Histogram", xlab="Number of daily steps")
abline(v=mean(hist_sum, na.rm=TRUE), col="red", lwd=2)
new_activity<-mutate(new_activity, weekday=weekdays(as.Date(new_activity$date)))
new_activity<-mutate(new_activity, weekend= if_else(weekday=="sábado" | weekday=="domingo", TRUE, FALSE))
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). The plot should look something like the following, which was created using simulated data:First of all we need to get the information on the average of steps by interval accross all days.
activity_weekend<-filter(new_activity, weekend==TRUE)
activity_weekday<-filter(new_activity, weekend==FALSE)
a_by_interval_weekday<-activity_weekday %>%
group_by(interval) %>%
summarise(avg_steps=mean(steps, na.rm=T))
a_by_interval_weekend<-activity_weekend %>%
group_by(interval) %>%
summarise(avg_steps=mean(steps, na.rm=T))
And then plot it to be able to compare the behaviour weekend vs weekday
par(mfrow=c(2,1))
with(a_by_interval_weekday, plot(avg_steps ~ interval, type="l"))
with(a_by_interval_weekend, plot(avg_steps ~ interval, type="l", ylim=c(0,250)))