The data from a personal activity monitoring device. This device collects data at 5 minute intervals through out the day. The data consists of two months of data from an anonymous individual collected during the months of October and November, 2012 and include the number of steps taken in 5 minute intervals each day.
fileUrl<-"https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip"
download.file(fileUrl,destfile="./data1/Dataset.zip")
unzip(zipfile="./data1/Dataset.zip",exdir="./data1")
path_rf <- file.path("./data1" , "data1.csv")
files<-list.files(path_rf, recursive=TRUE)
act<-read.csv("./data1/activity.csv")
stepperday<-tapply(act$steps,act$date,sum,na.rm=T)
hist(stepperday, xlab = "number of steps",
main = "the total number of steps taken each day")
summary(stepperday)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0 6778 10395 9354 12811 21194
mean_interval <- tapply(act$steps, act$interval, mean, na.rm = T)
plot(mean_interval, type = "l", main = "time series plot", xlab = "the 5-minute interval", ylab = "the average number of steps")
which.max(mean_interval)
## 835
## 104
stepsNA <- sum(is.na(act$steps))
dateNA <- sum(is.na(act$date))
intervalNA <- sum(is.na(act$interval))
numMissingValues <- length(which(is.na(act$steps)))
numMissingValues
## [1] 2304
impute <- function(x, x.impute){ifelse(is.na(x),x.impute,x)}
act2 <- act
act2$steps <- impute(act$steps, mean(act$steps))
act2step <- tapply(act2$steps, act2$date, sum,na.rm=TRUE)
hist(act2step, xlab='Total steps per day (Imputed)',bins=50)
## Warning in plot.window(xlim, ylim, "", ...): "bins"不是图形参数
## Warning in title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...):
## "bins"不是图形参数
## Warning in axis(1, ...): "bins"不是图形参数
## Warning in axis(2, ...): "bins"不是图形参数
act2$dateType <- ifelse(as.POSIXlt(act2$date)$wday %in% c(0,6), 'weekend', 'weekday')
library(ggplot2)
avgAct2 <- aggregate(steps ~ interval + dateType, data=act2, mean)
ggplot(avgAct2, aes(interval, steps)) +
geom_line() +
facet_grid(dateType ~ .) +
xlab("5-minute interval") +
ylab("avarage number of steps")