Peer-graded Assignment: Course Project 1

Loading and preprocessing the data

data <- read.csv("activity.csv")
summary(data)
##      steps                date          interval     
##  Min.   :  0.00   2012-10-01:  288   Min.   :   0.0  
##  1st Qu.:  0.00   2012-10-02:  288   1st Qu.: 588.8  
##  Median :  0.00   2012-10-03:  288   Median :1177.5  
##  Mean   : 37.38   2012-10-04:  288   Mean   :1177.5  
##  3rd Qu.: 12.00   2012-10-05:  288   3rd Qu.:1766.2  
##  Max.   :806.00   2012-10-06:  288   Max.   :2355.0  
##  NA's   :2304     (Other)   :15840

What is mean total number of steps taken per day?

1.Calculate the total number of steps taken per day

stepsPday <- aggregate(steps~date,data,sum,na.rm=TRUE)
head(stepsPday)
##         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

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

hist(stepsPday$steps)

3.Calculate and report the mean and median of the total number of steps taken per day

mean_stepsperday <- mean(stepsPday$steps)
mean_stepsperday
## [1] 10766.19
median_stepsperday <- median(stepsPday$steps)
median_stepsperday
## [1] 10765

What is the average daily activity pattern?

1.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)

library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.1
## 
## 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
stepsbyInterval <- summarize(group_by(data,interval),steps=mean(steps,na.rm=TRUE))
plot(steps~interval,data=stepsbyInterval,type='l',main="the average number of steps")

2.Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?

maxstepsbyInterval <- stepsbyInterval[which.max(stepsbyInterval$steps),]$interval
maxstepsbyInterval
## [1] 835

Imputing missing values

1.Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)

totalmissingdata <- sum(is.na(data$steps))
totalmissingdata
## [1] 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.

getmeansteps <- function(interval){
  stepsbyInterval[stepsbyInterval$interval==interval,]$steps
}

3.Create a new dataset that is equal to the original dataset but with the missing data filled in.

datanoNA <-data
for(i in 1:nrow(datanoNA)){
  if(is.na(datanoNA[i,]$steps)){
    datanoNA[i,]$steps <- getmeansteps(datanoNA[i,]$interval)
  }
}
head(datanoNA)
##       steps       date interval
## 1 1.7169811 2012-10-01        0
## 2 0.3396226 2012-10-01        5
## 3 0.1320755 2012-10-01       10
## 4 0.1509434 2012-10-01       15
## 5 0.0754717 2012-10-01       20
## 6 2.0943396 2012-10-01       25

4.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?

totstepsdatanoNA <- aggregate(steps~date,datanoNA,sum,na.rm=TRUE)
hist(totstepsdatanoNA$steps)

Are there differences in activity patterns between weekdays and weekends?

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

datanoNA$date <-as.Date(strptime(datanoNA$date,format="%Y-%m-%d"))
class(datanoNA$date)
## [1] "Date"
datanoNA$day <- weekdays(datanoNA$date)
for(i in 1:nrow(datanoNA)){
  if(datanoNA[i,]$day %in% c("토요일","일요일")){
    datanoNA[i,]$day <-"weekend"
  }else{
    datanoNA[i,]$day <-"weekday"
  }
}
stepsbyday <- aggregate(steps~interval+day,datanoNA,mean)
head(stepsbyday)
##   interval     day      steps
## 1        0 weekday 2.25115304
## 2        5 weekday 0.44528302
## 3       10 weekday 0.17316562
## 4       15 weekday 0.19790356
## 5       20 weekday 0.09895178
## 6       25 weekday 1.59035639

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

library(ggplot2)
g<-ggplot(stepsbyday,aes(interval,steps))
g+geom_line()+
  facet_grid(.~day)+
  labs(x="interval")+
  labs(y="steps")+
  labs(title="comparing the average number of steps by interval across weekdays and weekends")