Show any code that is needed to
Load the data (i.e. read.csv())
Process/transform the data (if necessary) into a format suitable for your analysis
# Load the data file into a data frame
activity <- read.csv("activity.csv", as.is = TRUE)
# Remove the NA values and store in a separate structure for future use
good_act <- activity[complete.cases(activity), ]
For this part of the assignment, you can ignore the missing values in the dataset.
Calculate the total number of steps taken per day
If you do not understand the difference between a histogram and a barplot, research the difference between them. Make a histogram of the total number of steps taken each day
Calculate and report the mean and median of the total number of steps taken per day
# Calculate the total number of steps taken per day
steps_per_day <- aggregate(steps ~ date, good_act, sum)
# Create a histogram of no of steps per day
hist(steps_per_day$steps, main = "Histogram of total number of steps per day", xlab = "Steps per day")
# Calculate the mean and median of the total number of steps taken per day
round(mean(steps_per_day$steps))
## [1] 10766
median(steps_per_day$steps)
## [1] 10765
Make 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 days (y-axis)
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
# Calculate average steps per interval for all days
avg_steps_per_interval <- aggregate(steps ~ interval, good_act, mean)
# Calculate average steps per day for all intervals - Not required, but for my own sake
avg_steps_per_day <- aggregate(steps ~ date, good_act, mean)
# Plot the time series with appropriate labels and heading
plot(avg_steps_per_interval$interval, avg_steps_per_interval$steps, type='l', col=1, main="Average number of steps by Interval", xlab="Time Intervals", ylab="Average number of steps")
# Identify the interval index which has the highest average steps
interval_idx <- which.max(avg_steps_per_interval$steps)
# Identify the specific interval and the average steps for that interval
print (paste("The interval with the highest avg steps is ", avg_steps_per_interval[interval_idx, ]$interval, " and the no of steps for that interval is ", round(avg_steps_per_interval[interval_idx, ]$steps, digits = 1)))
## [1] "The interval with the highest avg steps is 835 and the no of steps for that interval is 206.2"
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?
# Calculate the number of rows with missing values
missing_value_act <- activity[!complete.cases(activity), ]
nrow(missing_value_act)
## [1] 2304
# Loop thru all the rows of activity, find the one with NA for steps.
# For each identify the interval for that row
# Then identify the avg steps for that interval in avg_steps_per_interval
# Substitute the NA value with that value
for (i in 1:nrow(activity)) {
if(is.na(activity$steps[i])) {
val <- avg_steps_per_interval$steps[which(avg_steps_per_interval$interval == activity$interval[i])]
activity$steps[i] <- val
}
}
# Aggregate the steps per day with the imputed values
steps_per_day_impute <- aggregate(steps ~ date, activity, sum)
# Draw a histogram of the value
hist(steps_per_day_impute$steps, main = "Histogram of total number of steps per day (IMPUTED)", xlab = "Steps per day")
# Compute the mean and median of the imputed value
# Calculate the mean and median of the total number of steps taken per day
round(mean(steps_per_day_impute$steps))
## [1] 10766
median(steps_per_day_impute$steps)
## [1] 10766.19
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.
week_day <- function(date_val) {
wd <- weekdays(as.Date(date_val, '%Y-%m-%d'))
if (!(wd == 'Saturday' || wd == 'Sunday')) {
x <- 'Weekday'
} else {
x <- 'Weekend'
}
x
}
Apply the function to the dataset to create a new day type variable
# Apply the week_day function and add a new column to activity dataset
activity$day_type <- as.factor(sapply(activity$date, week_day))
#load the ggplot library
library(ggplot2)
# Create the aggregated data frame by intervals and day_type
steps_per_day_impute <- aggregate(steps ~ interval+day_type, activity, mean)
# Create the plot
plt <- ggplot(steps_per_day_impute, aes(interval, steps)) +
geom_line(stat = "identity", aes(colour = day_type)) +
theme_gray() +
facet_grid(day_type ~ ., scales="fixed", space="fixed") +
labs(x="Interval", y=expression("No of Steps")) +
ggtitle("No of steps Per Interval by day type")
print(plt)