This file was created to answer the questions of the course projecto 1 of the Reproductible Research course.
activity <- read.csv("activity.csv")
activity$date <- as.Date(activity$date)
# Aggregate the sum of steps by day
steps_day <- aggregate(steps ~ date, data = activity, FUN = sum, na.rm = T)
# Create characters containing the mean an median of the steps by day
mean_steps <- as.character(round(mean(steps_day$steps)))
median_steps <- as.character(round(median(steps_day$steps)))
# Create a histogram of the number of steps taken by day
hist(steps_day$steps, main = "Histogram of the number of steps taken per day", xlab = "Number of steps")
abline(v = mean(steps_day$steps), col = "red", lwd = 3) # Vertical line with the mean
abline(v = median(steps_day$steps), col = "blue", lwd = 2) # Vertival line with the median
Figure 1. Number of steps taken by day. The blue line represent the median (10765) by day and the red line represent the mean (10766) by day.
# Aggregate the mena of steps by interval of 5 minuntes.
steps_interval <- aggregate(steps ~ interval, data = activity, FUN = mean, na.rm = T)
# Plot the data using ggplot2
ggplot(steps_interval, aes(x = interval, y = steps)) + geom_line()
Figure 2. Average number of steps taken in 5-minute interval, averaged across all days
# Averaged interval
interval <- subset(steps_interval, steps == max(steps_interval$steps))$interval
The 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps is 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.
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?
number_of_na <- nrow(subset(activity, is.na(steps)))
The number of missing values in the data set is 2304. To filling the missing values, the mean of the 5-minute interval will be calculated and the NA’s assigned with the mean value fot the that interval.
# Create a data set with the mean values of steps for each interval value
interval_mean <- aggregate(steps ~ interval, data = activity, FUN = mean)
# Merge the interval_mean data set with the activity table where the steps are NA's and assign in a new data frame called activity_filled_na
activity_filled_na <- merge(activity[is.na(activity$steps), c("date", "interval")], interval_mean, by = "interval")
# Combine the rows of the new activity_filled_na with the activity where the steps are not NA's.
activity_filled_na <- rbind(activity_filled_na, activity[!is.na(activity$steps),])
# Calculate the total steps by day
total_steps_day <- aggregate(steps ~ date, data = activity_filled_na, FUN = sum)
# Histogram with the total steps by day
hist(total_steps_day$steps)
abline(v = mean(total_steps_day$steps), lwd = 3, col = "red") # Red vertival line with the mean
abline(v = median(total_steps_day$steps), lwd = 2, col = "blue") # Blue vertival line with the median
# Create characters containing the mean an median of the steps by day
mean_steps <- as.character(round(mean(total_steps_day$steps)))
median_steps <- as.character(round(median(total_steps_day$steps)))
Figure 3. Number of steps taken by day. The blue line represent the median (10766) by day and the red line represent the mean (10766) by day.
# Calculate the diference betwen the mean between the data wiht the missin values and without the missing values
mean_dif <- as.character(round((mean(total_steps_day$steps) - mean(steps_day$steps)), 2))
# Calculate the diference betwen the median between the data wiht the missin values and without the missing values
median_dif <- as.character(round(median((total_steps_day$steps) - median(steps_day$steps)), 2))
The diference between the mean of the data where the NA’s values was removed and the data where the missing values was filled with the mean of the correspond interval was 0. And the same diference in the median was 1.19. The means are equal but the median was different.
For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part.
Create a new factor variable in the dataset with two levels – “weekday” and “weekend” indicating whether a given date is a weekday or weekend day.
Make a 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). See the README file in the GitHub repository to see an example of what this plot should look like using simulated data.
# Create a new variable with the weekdays and weekends
activity$weekday <- ifelse(weekdays(activity$date) %in% c("Saturday","Sunday"), "weekend", "weekday")
# Create the averaged data
activity_avraged <- aggregate(steps ~ interval + weekday, data = activity, FUN = mean)
# Plot the data
ggplot(data = activity_avraged, aes(x = interval, y = steps, colour = weekday)) +
geom_line() +
facet_wrap(~weekday, ncol = 1)
Figure 4. Average number of steps taken in weekday and weekends.