This file was created to answer the questions of the course projecto 1 of the Reproductible Research course.

Loading and preprocessing the data

activity <- read.csv("activity.csv")
activity$date <- as.Date(activity$date)

What is mean total number of steps taken per day?

# 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.

What is the average daily activity pattern?

# 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.

Imputing missing values

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.

Are there differences in activity patterns between weekdays and weekends?

# 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.