Loading and preprocessing the data

Show any code that is needed to
1. Load the data (i.e. read.csv())
2. Process/transform the data (if necessary) into a format suitable for your analysis

activity <- read.csv("activity.csv")

What is mean total number of steps taken per day?

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
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
3. Calculate and report the mean and median of the total number of steps taken per day

stepstotal <- aggregate(steps~date, data = activity, sum, na.action = na.omit)
hist(stepstotal$steps, xlab = "Total steps per day", ylab = "Number of days", main = "Histogram of total steps per day")

mean(stepstotal$steps); median(stepstotal$steps)
## [1] 10766.19
## [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)
  2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?
stepsmean <- aggregate(steps~interval, data = activity, mean, na.action = na.omit)
plot(stepsmean$interval, stepsmean$steps, type = "l", xlab = "Interval", ylab = "average steps by interval", main = "Average activity during all day")

stepsmean[which.max(stepsmean$steps),]
##     interval    steps
## 104      835 206.1698

Imputing missing values

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

sum(is.na(activity$steps))
## [1] 2304
mu <- mean(activity$steps, na.rm = T)
activityNA <- read.csv("activity.csv")
activityNA$steps[is.na(activityNA$steps)]<-mu
stepstotalNA <- aggregate(steps~date, data = activityNA, sum, na.action = na.omit)
hist(stepstotalNA$steps, xlab = "Total steps per day", ylab = "Number of days", main = "Histogram of total steps per day")

mean(stepstotalNA$steps); median(stepstotalNA$steps)
## [1] 10766.19
## [1] 10766.19

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

activityNA$date <- as.Date(activityNA$date, tryFormats = "%Y-%m-%d")
activityNA$weekdays <- weekdays(activityNA$date)
activityNA$DayOfWeek <- ifelse(activityNA$weekdays=="土曜日"|activityNA$weekdays=="日曜日", "weekend", "weekdays")
stepsmeanNEW <- aggregate(steps~interval+DayOfWeek, data = activityNA, mean)
library(ggplot2)
g <- ggplot(stepsmeanNEW, aes(interval, steps, color = DayOfWeek))
g + geom_line() + facet_grid(DayOfWeek~.)