Loading and preprocessing the data:

Sys.setlocale("LC_ALL","English")
## [1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252"
setwd("E:/Rtest/5.RR/Week1")
actdata <- read.csv("activity.csv",sep = ",")
str(actdata)
## 'data.frame':    17568 obs. of  3 variables:
##  $ steps   : int  NA NA NA NA NA NA NA NA NA NA ...
##  $ date    : Factor w/ 61 levels "2012-10-01","2012-10-02",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ interval: int  0 5 10 15 20 25 30 35 40 45 ...
actdata$date <- as.Date(actdata$date,"%Y-%m-%d")

What is mean total number of steps taken per day:

Calculate the total number of steps taken per day:

actdatanarm <- subset(actdata,steps != "NA")
TSaday <- with(actdatanarm,tapply(steps,date,sum))

Make a histogram of the total number of steps taken each day:

hist(TSaday,main="Total Steps per Day",xlab = "The Number of Total Steps",col="red")

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

mean(TSaday,na.rm=TRUE)
## [1] 10766.19
median(TSaday,na.rm=TRUE)
## [1] 10765

What is the average daily activity pattern:

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):

FiveMS <- with(actdatanarm,tapply(steps,interval,mean,na.rm=TRUE))
plot(FiveMS,type = "l",col="red",xlab = "Time Series",ylab = "Ave steps per time series", main = "The Average Steps taken per Time Series")

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

names(which.max(FiveMS))
## [1] "835"

Imputing missing values:

the total number of missing values:

sum(is.na(actdata))
## [1] 2304

Devise a strategy for filling in all of the missing values in the dataset and Create a new dataset that is equal to the original dataset but with the missing data filled in:

newactdata <- actdata
for(i in 1:nrow(newactdata)){
      if(is.na(newactdata[i,]$steps)){
            newactdata[i,]$steps <- subset(FiveMS, row.names(FiveMS)== as.character(newactdata[i,]$interval))[[1]]
      }
}
head(newactdata)
##       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
sum(is.na(newactdata))
## [1] 0

Make a histogram of the total number of steps taken each day:

newTSaday <- with(newactdata,tapply(steps,date,sum))
par(mfrow = c(1,2))
hist(TSaday,main="Total Steps per Day",xlab = "The Number of Total Steps",col="red",breaks=10,ylim=c(1,30))
hist(newTSaday,main="New Total Steps per Day",xlab = "The New Number of Total Steps",col="magenta",breaks=10,ylim = c(1,30))

Obviously, these values are different from the estimates from the first part of the assignment.

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

mean(newTSaday)
## [1] 10766.19
median(newTSaday)
## [1] 10766.19

After imputing missing data on the estimates of the total daily number of steps, the median becomes smaller, but the mean remains, and the mdian is equal to the mean.

Are there differences in activity patterns between weekdays and weekends:

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:

for(i in 1:nrow(newactdata)){
      if(weekdays(newactdata[i,]$date) %in% c("Sunday","Saturday")){
            newactdata$NewVal[i] <- "Weekends"
      }
      else{
            newactdata$NewVal[i] <- "Weekdays"
      }
}
newactdata <- transform(newactdata, NewVal = as.factor(NewVal))
table(newactdata$NewVal)
## 
## Weekdays Weekends 
##    12960     4608

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):

wdas <- subset(newactdata,NewVal == "Weekdays")
weds <- subset(newactdata,NewVal == "Weekends")
par(mfrow = c(1,1))

weekdays plot:

wdasFiveMS <- with(wdas,tapply(steps,interval,mean,na.rm=TRUE))
plot(wdasFiveMS,type = "l",col="red",xlab = "Weekdays Time Series",ylab = "Ave steps per time series- Weekdays", main = "The Average Steps taken per Time Series on Weekdays")

weekends plot:

wedsFiveMS <- with(weds,tapply(steps,interval,mean,na.rm=TRUE))
plot(wedsFiveMS,type = "l",col="magenta",xlab = "Weekends Time Series",ylab = "Ave steps per time series- Weekends", main = "The Average Steps taken per Time Series on Weekends")