forked from rdpeng/RepData_PeerAssessment1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPeerAssessment1.R
164 lines (123 loc) · 5.94 KB
/
PeerAssessment1.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# A.Loading and preprocessing the data
setwd("~/GitHub/RepData_PeerAssessment1")
data <- read.csv("activity.csv")
library(lattice) #used for xyplot
#exploring data
names(data)
str(data)
summary(data$steps)
unique(data$steps)
summary(data$date)
length(unique(data$date))
summary(data$interval)
unique(data$interval)
#------------------------------------------------------------
# data cleansing
refined.data <- data[!(is.na(data$steps)), ]
str(refined.data)
summary(refined.data$steps)
#------------------------------------------------------------
#B.What is mean total number of steps taken per day?
sum.data <- aggregate(steps ~ date , data=refined.data, FUN=sum)
names(sum.data) <- c("date","sum.steps")
#Figure 1
xyplot(sum.data $sum.steps ~ sum.data $date ,
layout = c(1,1),type="h",xlab="Date",ylab="Total Number of Steps")
mean.data <- aggregate(steps ~ date , data=refined.data, FUN=mean)
names(mean.data) <- c("date","mean.steps")
head(mean.data)
median.data <- aggregate(steps ~ date , data=refined.data, FUN=c("median"))
names(median.data) <- c("date","median.steps")
head(median.data)
#------------------------------------------------------------
#C. 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)
meaninterval.data <- aggregate(steps ~ interval , data=data, FUN=mean)
names(meaninterval.data) <- c("interval", "mean.steps")
#Figure 2
xyplot(meaninterval.data $mean.steps ~ meaninterval.data $interval ,
layout = c(1,1),type="l",xlab="Interval",ylab="Number of Steps")
#2.Which 5-minute interval, on average across all the days in the dataset, contains
#the maximum number of steps?
meaninterval.data[meaninterval.data$mean.steps==max(meaninterval.data $mean.steps),1]
#----------------------------------------------------------------
#D. 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)
NA.data <- data[is.na(data$steps), ]
nrow(NA.data)
#----------------------------------------
#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.
# Method 3.1
filled.data <- data
# check column names
intersect (names(mean.data),names(filled.data))
#join two tables- bring in mean steps per day to the original database
merged.data <- merge(mean.data,filled.data,all=TRUE)
#Replacing NA values with mean per day
merged.data[is.na(merged.data$steps),]$steps <- merged.data[is.na(merged.data$steps),]$mean.steps
#checking operation
NA.mergeddata <- merged.data[is.na(merged.data$steps), ]
refined.data <- data[!(is.na(data$steps)), ]
merged.data[(is.na(merged.data$steps)) & !(is.na(merged.data$mean.steps)), ]
nrow(NA.mergeddata)
#This method did not work out. let's try another method!
#---------------------------------------------
# Method 3.2
filledinterval.data <- data
meaninterval.data <- aggregate(steps ~ interval , data=data, FUN=mean)
names(meaninterval.data) <- c("interval", "mean.steps")
# check column names
intersect (names(meaninterval.data),names(filledinterval.data <- data ))
#join two tables- bring in mean steps per interval to the original database
mergedinterval.data <- merge(meaninterval.data,filledinterval.data,all=TRUE)
#Replacing NA values with mean per interval
mergedinterval.data[is.na(mergedinterval.data$steps),]$steps <-
mergedinterval.data[is.na(mergedinterval.data$steps),]$mean.steps
#checking operation
NA.mergedintervaldata <- mergedinterval.data[is.na(mergedinterval.data$steps), ]
nrow(NA.mergedintervaldata) # 0 rows OK - All NULL values are filled up
#-----------------------------------------------------------------
# 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?
newsum.data <- aggregate(steps ~ date , data=mergedinterval.data, FUN=sum)
names(newsum.data) <- c("date","newsum.steps")
# par(mfrow = c(2, 1))
# plot(sum.data)
# plot(newsum.data)
#Figure 3
xyplot(newsum.data $newsum.steps ~ newsum.data $date
,type="h",xlab="Date",ylab="Total Number of Steps")
newmean.data <- aggregate(steps ~ date , data=mergedinterval.data, FUN=mean)
names(newmean.data) <- c("date","newmean.steps")
head (newmean.data)
newmedian.data <- aggregate(steps ~ date , data=mergedinterval.data, FUN=c("median"))
names(newmedian.data) <- c("date","newmedian.steps")
head (newmedian.data)
#----------------------------------------------------------------------------------------------
#E. Are there differences in activity patterns between weekdays and weekends?
#creating an empty column
refined.data $daytype <- NA
#weekdays(as.POSIXlt("2012-10-02"))
#as.POSIXlt(refined.data[i, ]$date)$wday returns day of week from 0 to 6
for ( i in 1:nrow(refined.data) )
ifelse( as.POSIXlt(refined.data[i, ]$date)$wday %in% c("5","6"),
refined.data[i,] $daytype <- "weekend",
refined.data[i,] $daytype <- "weekday"
)
#unique(refined.data$daytype)
#unique(as.POSIXlt(refined.data$date)$wday)
#class(refined.data$daytype)
#summary(refined.data$daytype)
# converting to factor
refined.data$daytype <- as.factor(refined.data$daytype)
#----------------------------------------------------------------
# Figure 4
xyplot(refined.data$steps ~ refined.data$interval | refined.data$daytype,
layout = c(1,2),type="l",xlab="Interval",ylab="Number of Steps")