library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
##
## 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
library(tibble)
library(lattice)
activity <- read.csv("activity.csv")
str(activity)
## 'data.frame': 17568 obs. of 3 variables:
## $ steps : int NA NA NA NA NA NA NA NA NA NA ...
## $ date : chr "2012-10-01" "2012-10-01" "2012-10-01" "2012-10-01" ...
## $ interval: int 0 5 10 15 20 25 30 35 40 45 ...
What is mean total number of steps taken per day?
totalbyday <- aggregate(steps~date, activity, sum)
head(totalbyday)
## date steps
## 1 2012-10-02 126
## 2 2012-10-03 11352
## 3 2012-10-04 12116
## 4 2012-10-05 13294
## 5 2012-10-06 15420
## 6 2012-10-07 11015
mean_day <- aggregate(steps~date, activity, mean)
head(mean_day)
## date steps
## 1 2012-10-02 0.43750
## 2 2012-10-03 39.41667
## 3 2012-10-04 42.06944
## 4 2012-10-05 46.15972
## 5 2012-10-06 53.54167
## 6 2012-10-07 38.24653
hist(totalbyday$steps,
col = "turquoise",
main = "Total Number of Steps Taken Each Day",
xlab = "Steps",
ylab = "Number of Days")
3.1 Calculate mean
mean_steps <- mean(totalbyday$steps)
print(mean_steps)
## [1] 10766.19
3.2 Calulate median
median_steps <- median(totalbyday$steps)
print(median_steps)
## [1] 10765
What is the average daily activity pattern?
1.1 The average number of steps taken:
average_steps <- aggregate(steps~interval, activity, mean)
head(average_steps)
## interval steps
## 1 0 1.7169811
## 2 5 0.3396226
## 3 10 0.1320755
## 4 15 0.1509434
## 5 20 0.0754717
## 6 25 2.0943396
1.2 Time series plot
with(average_steps,
plot(interval,
steps,
type = "l",
col= "red",
main = "Time Series Plot"))
2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
interval_max <- average_steps[which.max(average_steps$steps),1]
print(interval_max)
## [1] 835
missing_values <- sum(is.na(activity$steps))
print(missing_values)
## [1] 2304
Answer 1: The total number of rows with NAs is 2304)
2.1 Averaging: the mean number of steps per Interval
mean_values <- mean(average_steps$steps)
print(mean_values)
## [1] 37.3826
3.1 Create a new dataset
new_df <- activity
3.2 Fill the new_df with mean values (37.3826)
new_df[is.na(new_df)] <- 37.3826
head(new_df)
## steps date interval
## 1 37.3826 2012-10-01 0
## 2 37.3826 2012-10-01 5
## 3 37.3826 2012-10-01 10
## 4 37.3826 2012-10-01 15
## 5 37.3826 2012-10-01 20
## 6 37.3826 2012-10-01 25
totalstepsnew_df <- aggregate(steps ~ date, new_df, sum)
hist(totalstepsnew_df$steps,
main = "Total Steps Taken Each day - Replacing NA",
ylab = "Number of Days",
xlab = "Steps",
col = "pink")
4.1 Calculate mean
mean_steps_new_df <- mean(totalstepsnew_df$steps)
head(mean_steps_new_df)
## [1] 10766.19
4.2 Calulate median
median_steps_new_df <- median(totalstepsnew_df$steps)
print(median_steps_new_df)
## [1] 10766.19
Do these values differ from # the estimates from the first part of the assignment? There is insignificant difference. When we replacing the NA values we got a increase about number of days but insignificant difference between previous values of mean and median
What is the impact of inputing missing data on the estimates of the total daily number of steps? The number of days increased.
Are there differences in activity patterns between weekdays and weekends? For this part the weekdays() function may be of some help here. Use the dataset with the filled-in missing values for this part.
new_df$date <- as.Date(new_df$date)
df <- new_df %>%
mutate(dayofweek = ifelse(weekdays(new_df$date) == "Saturday" | weekdays(new_df$date) == "Sunday",
"weekend","weekday"))
df2<-df %>%
group_by(dayofweek, interval) %>%
summarize(sumsteps=sum(steps))
## `summarise()` regrouping output by 'dayofweek' (override with `.groups` argument)
head(df2)
## # A tibble: 6 x 3
## # Groups: dayofweek [1]
## dayofweek interval sumsteps
## <chr> <int> <dbl>
## 1 weekday 0 315.
## 2 weekday 5 242.
## 3 weekday 10 231.
## 4 weekday 15 232.
## 5 weekday 20 228.
## 6 weekday 25 283.
with(df2,
xyplot(sumsteps ~ interval | dayofweek,
type = "l",
main = "Total Number of Steps within Intervals by dayofweek",
xlab = "Daily Intervals",
ylab = "Average Number of Steps"))