Chia-wen Kao
It is now possible to collect a large amount of data about personal movement using activity monitoring devices such as a Fitbit, Nike Fuelband, or Jawbone Up. These type of devices are part of the “quantified self” movement – a group of enthusiasts who take measurements about themselves regularly to improve their health, to find patterns in their behavior, or because they are tech geeks. But these data remain under-utilized both because the raw data are hard to obtain and there is a lack of statistical methods and software for processing and interpreting the data.
This assignment makes use of data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
The data for this assignment can be downloaded from the course web site:
Dataset: Activity monitoring data [52K] The variables included in this dataset are:
The dataset is stored in a comma-separated-value (CSV) file and there are a total of 17,568 observations in this dataset.
setwd("/Users/test//Desktop/Rcode")
activity <- read.csv("activity.csv")
activity$date <- as.Date(activity$date)
head(activity)
## steps date interval
## 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
For this part of the assignment, you can ignore the missing values in the dataset. 1. Calculate the total number of steps taken per day
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
stepsDay <- activity %>%
group_by(date) %>%
summarise(sumsteps = sum(steps, na.rm = TRUE))
## `summarise()` ungrouping output (override with `.groups` argument)
#First 10 rows:
head(stepsDay, 10)
## # A tibble: 10 x 2
## date sumsteps
## <date> <int>
## 1 2012-10-01 0
## 2 2012-10-02 126
## 3 2012-10-03 11352
## 4 2012-10-04 12116
## 5 2012-10-05 13294
## 6 2012-10-06 15420
## 7 2012-10-07 11015
## 8 2012-10-08 0
## 9 2012-10-09 12811
## 10 2012-10-10 9900
hist(stepsDay$sumsteps, main = "Histogram of Daily Steps", col = "green", xlab = "Steps", ylim = c(0, 30))
meanstep <- mean(stepsDay$sumsteps)
medianstep <- median(stepsDay$sumsteps)
print(paste("The mean is: ", meanstep))
## [1] "The mean is: 9354.22950819672"
print(paste("The median is: ", medianstep))
## [1] "The median is: 10395"
stepsInterval <- activity %>%
group_by(interval) %>%
summarise(meansteps = mean(steps, na.rm = TRUE))
## `summarise()` ungrouping output (override with `.groups` argument)
#First 10 rows:
head(stepsInterval, 10)
## # A tibble: 10 x 2
## interval meansteps
## <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
plot(stepsInterval$meansteps ~ stepsInterval$interval, col = "red", type = "l",
xlab = "5-minute interval", ylab = "Average Number of Steps", main = "Daily Mean Steps by Interval" )
print(paste("Interval containing the most steps on average: ",stepsInterval$interval[which.max(stepsInterval$meansteps)]))
## [1] "Interval containing the most steps on average: 835"
print(paste("Average steps for that interval: ",max(stepsInterval$meansteps)))
## [1] "Average steps for that interval: 206.169811320755"
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. 1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
print(paste("The total number of rows with NA is: ",sum(is.na(activity$steps))))
## [1] "The total number of rows with NA is: 2304"
2.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.
3.Create a new dataset that is equal to the original dataset but with the missing data filled in.
replacewithmean <- function(x) replace(x, is.na(x), mean(x, na.rm = TRUE))
meandata <- activity%>%
group_by(interval) %>%
mutate(steps= replacewithmean(steps))
head(meandata)
## # A tibble: 6 x 3
## # Groups: interval [6]
## steps date interval
## <dbl> <date> <int>
## 1 1.72 2012-10-01 0
## 2 0.340 2012-10-01 5
## 3 0.132 2012-10-01 10
## 4 0.151 2012-10-01 15
## 5 0.0755 2012-10-01 20
## 6 2.09 2012-10-01 25
stepsDay <- meandata %>%
group_by(date) %>%
summarise(sumsteps = sum(steps, na.rm = TRUE))
## `summarise()` ungrouping output (override with `.groups` argument)
head(stepsDay , 10)
## # A tibble: 10 x 2
## date sumsteps
## <date> <dbl>
## 1 2012-10-01 10766.
## 2 2012-10-02 126
## 3 2012-10-03 11352
## 4 2012-10-04 12116
## 5 2012-10-05 13294
## 6 2012-10-06 15420
## 7 2012-10-07 11015
## 8 2012-10-08 10766.
## 9 2012-10-09 12811
## 10 2012-10-10 9900
hist(stepsDay$sumsteps, main = "Histogram of Daily Steps",
col="green", xlab="Steps")
meanstep2 <- mean(stepsDay$sumsteps)
medianstep2 <- median(stepsDay$sumsteps)
print(paste("The mean is: ", meanstep2))
## [1] "The mean is: 10766.1886792453"
print(paste("The median is: ", medianstep2))
## [1] "The median is: 10766.1886792453"
Comparison:
comparison <- data.frame(mean = c(meanstep, meanstep2), median = c(medianstep, medianstep2))
rownames(comparison) <- c("Pre NA Transformation", "Post NA Transformation")
print(comparison)
## mean median
## Pre NA Transformation 9354.23 10395.00
## Post NA Transformation 10766.19 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.
activityDoW <- meandata
activityDoW$date <- as.Date(activityDoW$date)
activityDoW$day <- ifelse(weekdays(activityDoW$date) %in% c("Saturday", "Sunday"), "weekend", "weekday")
activityDoW$day <- as.factor(activityDoW$day)
meandata$date <- as.Date(meandata$date)
meandata$weekday <- weekdays(meandata$date)
meandata$weekend <- ifelse(meandata$weekday=="Saturday" | meandata$weekday=="Sunday", "Weekend", "Weekday" )
library(ggplot2)
meandataweekendweekday <- aggregate(meandata$steps , by= list(meandata$weekend, meandata$interval), na.omit(mean))
names(meandataweekendweekday) <- c("weekend", "interval", "steps")
ggplot(meandataweekendweekday, aes(x=interval, y=steps, color=weekend)) + geom_line()+
facet_grid(weekend ~.) + xlab("Interval") + ylab("Mean of Steps") +
ggtitle("Comparison of Average Number of Steps in Each Interval")