You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#1. Load the data (i.e. read.csv())
setwd("~/Documents/git/RepData_PeerAssessment1")
if (!file.exists("./activity.csv")) {
download.file("https://d396qusza40orc.cloudfront.net/repdata%2Fdata%2Factivity.zip", "./activity.zip")
unzip("./activity.zip", overwrite=T, exdir=".")
}
activity<- read.csv("./activity.csv", header=T, sep=",")
#2. Process/transform the data (if necessary) into a format suitable for your analysisactivity$date<- as.Date(activity$date)
What is mean total number of steps taken per day?
#1. Calculate the total number of steps taken per daysu<- tapply(activity$steps, activity$date, sum, na.rm=T)
su
#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
hist(su, xlab="sum of steps per day", main="histogram of steps per day")
#3. Calculate and report the mean and median of the total number of steps taken per daymean_su<- round(mean(su))
median_su<- round(median(su))
print(c("The mean is",mean_su))
## [1] "The mean is" "9354"
print(c("The median is",median_su))
## [1] "The median is" "10395"
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)mn_int<- tapply(activity$steps,
activity$interval,
mean,
na.rm=T)
plot(mn_int~ unique(activity$interval),
type="l",
xlab="5-min interval")
#2. Which 5-minute interval, on average across all the days in the dataset, contains the maximum number of steps?mn_int[which.max(mn_int)]
## 835
## 206.1698
Imputing missing values
#1. Calculate and report the total number of missing values in the dataset (i.e. the total number of rows with NAs)
sum(is.na(activity) ==TRUE)
## [1] 2304
#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.activity2<-activity# creation of the dataset that will have no more NAsfor (iin1:nrow(activity)){
if(is.na(activity$steps[i])){
activity2$steps[i]<-mn_int[[as.character(activity[i, "interval"])]]
}
}
#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?su2<- tapply(activity2$steps,
activity2$date,
sum,
na.rm=T)
hist(su2,
xlab="sum of steps per day",
main="histogram of steps per day")
mean_su2<- round(mean(su2))
median_su2<- round(median(su2))
print(c("The mean is",mean_su2))
## mean median
## with NA's 9354 10395
## without NA's 10766 10766
Are there differences in activity patterns between weekdays and weekends?
#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.activity2$weekday<- c("weekday")
activity2[weekdays(as.Date(activity2[, 2])) %in%
c("Saturday", "Sunday", "samedi", "dimanche", "saturday",
"sunday", "Samedi", "Dimanche"), ][4] <- c("weekend")
activity2$weekday<-factor(activity2$weekday)
#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.activity2_weekend<- subset(activity2,
activity2$weekday=="weekend")
activity2_weekday<- subset(activity2,
activity2$weekday=="weekday")
mean_activity2_weekday<- tapply(activity2_weekday$steps,
activity2_weekday$interval,
mean)
mean_activity2_weekend<- tapply(activity2_weekend$steps,
activity2_weekend$interval,
mean)
library(lattice)
df_weekday<-NULLdf_weekend<-NULLdf_final<-NULLdf_weekday<-data.frame(interval= unique(activity2_weekday$interval),
avg= as.numeric(mean_activity2_weekday),
day= rep("weekday", length(mean_activity2_weekday)))
df_weekend<-data.frame(interval= unique(activity2_weekend$interval),
avg= as.numeric(mean_activity2_weekend),
day= rep("weekend",
length(mean_activity2_weekend)))
df_final<- rbind(df_weekday, df_weekend)
xyplot(avg~interval|day,
data=df_final,
layout= c(1, 2),
type="l",
ylab="Number of steps")