Show any code that is needed to
1.Load data
2.Process/transform the data (if necessary) into a format suitable for your analysis
start by loading dplyr package
## Warning: package 'dplyr' was built under R version 3.4.4
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
activity <- read.csv("activity.csv")
activity <- tibble::as_tibble(activity)
activityCompleteObs <- activity[which(complete.cases(activity)),]
A view of activity
activity
## # A tibble: 17,568 x 3
## steps date interval
## <int> <fct> <int>
## 1 NA 2012-10-01 0
## 2 NA 2012-10-01 5
## 3 NA 2012-10-01 10
## 4 NA 2012-10-01 15
## 5 NA 2012-10-01 20
## 6 NA 2012-10-01 25
## 7 NA 2012-10-01 30
## 8 NA 2012-10-01 35
## 9 NA 2012-10-01 40
## 10 NA 2012-10-01 45
## # ... with 17,558 more rows
view activityCompleteObs
activityCompleteObs
## # A tibble: 15,264 x 3
## steps date interval
## <int> <fct> <int>
## 1 0 2012-10-02 0
## 2 0 2012-10-02 5
## 3 0 2012-10-02 10
## 4 0 2012-10-02 15
## 5 0 2012-10-02 20
## 6 0 2012-10-02 25
## 7 0 2012-10-02 30
## 8 0 2012-10-02 35
## 9 0 2012-10-02 40
## 10 0 2012-10-02 45
## # ... with 15,254 more rows
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, activityCompleteObs, 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")
abline(v=mean(steps_per_day$steps), col='blue')
abline(v=median(steps_per_day$steps), col='red')
print(paste("The mean and median of the total number of steps taken per day are:",
mean(steps_per_day$steps, na.rm = TRUE), "and", median(steps_per_day$steps, na.rm = TRUE)))
## [1] "The mean and median of the total number of steps taken per day are: 10766.1886792453 and 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, activityCompleteObs, mean)
avg_steps_per_interval <- tibble::as_tibble(avg_steps_per_interval)
avg_steps_per_interval
## # A tibble: 288 x 2
## interval steps
## <int> <dbl>
## 1 0 1.72
## 2 5 0.340
## 3 10 0.132
## 4 15 0.151
## 5 20 0.0755
## 6 25 2.09
## 7 30 0.528
## 8 35 0.868
## 9 40 0
## 10 45 1.47
## # ... with 278 more rows
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)
# Plot the time series with appropriate labels and heading
plot(avg_steps_per_interval$interval, avg_steps_per_interval$steps, type='l', col="blue", main="Average number of steps by Interval", xlab="Time Intervals", ylab="Average number of steps")
Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
which.max(avg_steps_per_interval$steps)
## [1] 104
avg_steps_per_interval[104,]
## # A tibble: 1 x 2
## interval steps
## <int> <dbl>
## 1 835 206.
So the 5-minute interval, on average across all the days in the dataset, containing the maximum number of steps is 835 with an average of 206 steps
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.
table(complete.cases(activity))
##
## FALSE TRUE
## 2304 15264
So there is 2304 rows with missing values in our dataset
We will replace the missing NA values with the average steps in that interval across all the days
To nail it we will add a variable Indicator in our dataset. This variable will indicate us whether one row contain a missing values
Ind <- function(t){
x <- dim(length(t))
x[which(!is.na(t))] <- 1
x[which(is.na(t))] <- 0
return(x)
}
activity$indicator <- Ind(activity$steps)
activity
## # A tibble: 17,568 x 4
## steps date interval indicator
## <int> <fct> <int> <dbl>
## 1 NA 2012-10-01 0 0
## 2 NA 2012-10-01 5 0
## 3 NA 2012-10-01 10 0
## 4 NA 2012-10-01 15 0
## 5 NA 2012-10-01 20 0
## 6 NA 2012-10-01 25 0
## 7 NA 2012-10-01 30 0
## 8 NA 2012-10-01 35 0
## 9 NA 2012-10-01 40 0
## 10 NA 2012-10-01 45 0
## # ... with 17,558 more rows
Let’s now write a code to impute missing values by
Loop through 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 (activity$indicator[i]==0){
value_to_impute <- avg_steps_per_interval$steps[which(avg_steps_per_interval$interval == activity$interval[i])]
activity$steps[i] <- value_to_impute
}
}
let’s take a view to our filled dataset
activity
## # A tibble: 17,568 x 4
## steps date interval indicator
## <dbl> <fct> <int> <dbl>
## 1 1.72 2012-10-01 0 0
## 2 0.340 2012-10-01 5 0
## 3 0.132 2012-10-01 10 0
## 4 0.151 2012-10-01 15 0
## 5 0.0755 2012-10-01 20 0
## 6 2.09 2012-10-01 25 0
## 7 0.528 2012-10-01 30 0
## 8 0.868 2012-10-01 35 0
## 9 0 2012-10-01 40 0
## 10 1.47 2012-10-01 45 0
## # ... with 17,558 more rows
# 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")
abline(v=mean(steps_per_day$steps), col='blue')
# 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
After imputing missing values, mean and median don’t really change!
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.
activity$day <- weekdays(as.Date(as.character(activity$date),'%Y-%m-%d'))
table(activity$day)
##
## dimanche jeudi lundi mardi mercredi samedi vendredi
## 2304 2592 2592 2592 2592 2304 2592
activity$weekdays <- as.factor(ifelse(weekdays(as.Date(activity$date)) %in% c("samedi", "dimanche"), "weekend", "weekday"))
table(activity$weekdays)
##
## weekday weekend
## 12960 4608
We load the ggplot2 package
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.4.4
# Create the aggregated data frame by intervals and day_type
steps_per_day_type <- aggregate(steps ~ interval+weekdays, activity, mean)
g <- ggplot(steps_per_day_type, aes(interval, steps))
g+geom_line(aes(colour = weekdays)) +
facet_grid(weekdays ~ .) +
labs(x="Interval", y=expression("Number of steps")) +
ggtitle("Average number of steps taken, averaged across all weekday days or weekend days")
We do see some subtle differences between the average number of steps between weekdays and weekends. For instance, it appears that the user started a bit later on weekend mornings