This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2
## 1st Qu.:12.0 1st Qu.: 26
## Median :15.0 Median : 36
## Mean :15.4 Mean : 43
## 3rd Qu.:19.0 3rd Qu.: 56
## Max. :25.0 Max. :120
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
setwd("C:/")
activity <-read.csv("./activity.csv")
totalSteps <- aggregate(steps ~ date, data = activity, sum, na.rm = TRUE)
hist(totalSteps$steps,col="blue")
mean(totalSteps$steps)
## [1] 10766
median(totalSteps$steps)
## [1] 10765
stepsInterval <- aggregate(steps ~ interval, data = activity, mean, na.rm = TRUE)
plot(steps ~ interval, data = stepsInterval, type = "l", col="blue")
stepsInterval[which.max(stepsInterval$steps), ]$interval
## [1] 835
sum(is.na(activity$steps))
## [1] 2304
interval2steps <- function(interval) {
stepsInterval[stepsInterval$interval == interval, ]$steps
}
activityFilled <- activity
count = 0
for (i in 1:nrow(activityFilled)) {
if (is.na(activityFilled[i, ]$steps)) {
activityFilled[i, ]$steps <- interval2steps(activityFilled[i, ]$interval)
count = count + 1
}
}
cat("Total ", count, "NA values were filled.\n\r")
## Total 2304 NA values were filled.
##
totalSteps2 <- aggregate(steps ~ date, data = activityFilled, sum)
hist(totalSteps2$steps, col="pink")
mean(totalSteps$steps)
## [1] 10766
median(totalSteps$steps)
## [1] 10765
activityFilled$day = ifelse(as.POSIXlt(as.Date(activityFilled$date))$wday%%6 ==
0, "weekend", "weekday")
activityFilled$day = factor(activityFilled$day, levels = c("weekday", "weekend"))
stepsInterval2 = aggregate(steps ~ interval + day, activityFilled, mean)
library(lattice)
xyplot(steps ~ interval | factor(day), data = stepsInterval2, aspect = 1/2,
type = "l", col="green")