##Assignment Instructions 1.Code for reading in the dataset and/or processing the data 2.Histogram of the total number of steps taken each day 3.Mean and median number of steps taken each day 4.Time series plot of the average number of steps taken 5.The 5-minute interval that, on average, contains the maximum number of steps 6.Code to describe and show a strategy for imputing missing data 7.Histogram of the total number of steps taken each day after missing values are imputed 8.Panel plot comparing the average number of steps taken per 5-minute interval across weekdays and weekends 9.All of the R code needed to reproduce the results (numbers, plots, etc.) in the report

##Step 1 ##Code for reading in the dataset and/or processing the data

setwd("C:/Users/AGANITH/Desktop/coursera/module2/reproducible research/RepData_PeerAssessment1/activity")
activity<-read.csv("activity.csv")
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

What is mean number of steps taken per day?

activity_total_steps <- with(activity, aggregate(steps, by = list(date), FUN = sum, na.rm = TRUE))
names(activity_total_steps) <- c("date", "steps")

b <- hist(activity_total_steps$steps, main = "Total number of steps taken per day", xlab = "Total steps taken per day", col = "darkblue", ylim = c(0,20), breaks = seq(0,25000, by=2500))

print(b)
## $breaks
##  [1]     0  2500  5000  7500 10000 12500 15000 17500 20000 22500 25000
## 
## $counts
##  [1] 11  2  5  7 18 10  6  0  2  0
## 
## $density
##  [1] 7.213115e-05 1.311475e-05 3.278689e-05 4.590164e-05 1.180328e-04
##  [6] 6.557377e-05 3.934426e-05 0.000000e+00 1.311475e-05 0.000000e+00
## 
## $mids
##  [1]  1250  3750  6250  8750 11250 13750 16250 18750 21250 23750
## 
## $xname
## [1] "activity_total_steps$steps"
## 
## $equidist
## [1] TRUE
## 
## attr(,"class")
## [1] "histogram"

Here is the mean of the total number of steps taken per day:

mean(activity_total_steps$steps)
## [1] 9354.23

Here is the median of the total number of steps taken per day:

median(activity_total_steps$steps)
## [1] 10395

Here is the mean of the total number of steps taken per day:

mean(activity_total_steps$steps)
## [1] 9354.23
  1. What is the average daily activity pattern?
average_daily_activity <- aggregate(activity$steps, by=list(activity$interval), FUN=mean, na.rm=TRUE)
names(average_daily_activity) <- c("interval", "mean")
plot(average_daily_activity$interval, average_daily_activity$mean, type = "l", col="darkblue", lwd = 2, xlab="Interval", ylab="Average number of steps", main="Average number of steps per intervals")

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

average_daily_activity[which.max(average_daily_activity$mean), ]$interval
## [1] 835
  1. Imputing missing values
sum(is.na(activity$steps))
## [1] 2304

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.

imputed_steps <- average_daily_activity$mean[match(activity$interval, average_daily_activity$interval)]

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

activity_imputed <- transform(activity, steps = ifelse(is.na(activity$steps), yes = imputed_steps, no = activity$steps))
total_steps_imputed <- aggregate(steps ~ date, activity_imputed, sum)
names(total_steps_imputed) <- c("date", "daily_steps")

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?

hist(total_steps_imputed$daily_steps, col = "darkblue", xlab = "Total steps per day", ylim = c(0,30), main = "Total number of steps taken each day", breaks = seq(0,25000,by=2500))

Here is the mean of the total number of steps taken per day:

median(total_steps_imputed$daily_steps)
## [1] 10766.19
  1. 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.

activity$date <- as.Date(strptime(activity$date, format="%Y-%m-%d"))
activity$datetype <- sapply(activity$date, function(x) {
        if (weekdays(x) == "Sábado" | weekdays(x) =="Domingo") 
                {y <- "Weekend"} else 
                {y <- "Weekday"}
                y
        })

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)
activity$datetype <- sapply(activity$date, function(x) {
  if(weekdays(x) == "Saturday" | weekdays(x) == "Sunday")
  {y <- "Weekend"} else {y <- "Weekday"} 
  y
})
activity_by_date <- aggregate(steps~interval + datetype, activity, mean, type = "l", na.rm = TRUE)
plot<- ggplot(activity_by_date, aes(x = interval , y = steps, color = datetype)) +
  geom_line() +
  labs(title = "Average daily steps by type of date", x = "Interval", y = "Average number of steps") +
  facet_wrap(~datetype, ncol = 1, nrow=2)
print(plot)