-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
351 lines (262 loc) · 13.1 KB
/
run_analysis.R
File metadata and controls
351 lines (262 loc) · 13.1 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#load the datasets and merge test and train conditions
setwd("D:/Academic/DataScience/GettingCleaningData/FinalProject/getdata-projectfiles-UCI HAR Dataset/UCI HAR Dataset")
features <-read.table("features.txt")
setwd("train")
subject_train <-read.table("subject_train.txt")
X_train <- read.table("X_train.txt")
Y_train <-read.table("y_train.txt")
setwd("../test")
subject_test <-read.table("subject_test.txt")
X_test <- read.table("X_test.txt")
Y_test <-read.table("y_test.txt")
X_total = rbind(X_train, X_test)
Y_total =rbind(Y_train, Y_test)
rm(X_test)
rm(X_train)
rm(Y_test)
rm(Y_train)
subject_total <-rbind(subject_train, subject_test)
rm(subject_test)
rm(subject_train)
meanlocs <-grep("mean", features[,2])
stdlocs <-grep("std", features[,2])
alllocs <-append(meanlocs, stdlocs)
alllocs <-sort(alllocs)
Xmeanstds <-X_total[,alllocs]
#cleaning Y_total
Y_total$V1 <-sub("1", "walking", Y_total$V1)
Y_total$V1 <-sub("2", "walkingupstairs", Y_total$V1)
Y_total$V1 <-sub("3", "walkingdownstairs", Y_total$V1)
Y_total$V1 <-sub("4", "sitting", Y_total$V1)
Y_total$V1 <-sub("5", "standing", Y_total$V1)
Y_total$V1 <-sub("6", "laying", Y_total$V1)
subnames <-features[alllocs,]
subnames[,2] <-gsub("-","",subnames[,2])
subnames[,2] <-sub("\\(", "", subnames[,2])
subnames[,2] <-sub("\\)", "", subnames[,2])
subnames[,2] <-tolower(subnames[,2])
names(Xmeanstds) <-subnames[,2]
tidyset <-cbind(subject_total, Y_total, Xmeanstds)
rm(list=c("X_total", "Y_total", "Xmeanstds"))
names(tidyset)[1] <-"subject"
names(tidyset)[2] <-"activity"
tidyset$activity <-as.factor(tidyset$activity)
##--------------------------------
#cycling through data at the moment, there may be a better way to do this
xttmpmeanx <-xtabs(tbodyaccmeanx ~ subject + activity, aggregate(tbodyaccmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(tbodyaccmeany ~ subject + activity, aggregate(tbodyaccmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(tbodyaccmeanz ~ subject + activity, aggregate(tbodyaccmeanz~subject+activity, tidyset, mean))
newtidy <- data.frame(xttmpmeanx)
names(newtidy)[3] <-"tbodyaccmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[4] <-"tbodyaccmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[5] <-"tbodyaccmeanz"
##--------------------------------
xttmpmeanx <-xtabs(tgravityaccmeanx ~ subject + activity, aggregate(tgravityaccmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(tgravityaccmeany ~ subject + activity, aggregate(tgravityaccmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(tgravityaccmeanz ~ subject + activity, aggregate(tgravityaccmeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[6] <-"tgravityaccmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[7] <-"tgravityaccmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[8] <-"tgravityaccmeanz"
##--------------------------------
xttmpmeanx <-xtabs(tbodyaccjerkmeanx ~ subject + activity, aggregate(tbodyaccjerkmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(tbodyaccjerkmeany ~ subject + activity, aggregate(tbodyaccjerkmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(tbodyaccjerkmeanz ~ subject + activity, aggregate(tbodyaccjerkmeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[9] <-"tbodyaccjerkmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[10] <-"tbodyaccjerkmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[11] <-"tbodyaccjerkmeanz"
##--------------------------------
xttmpmeanx <-xtabs(tbodygyromeanx ~ subject + activity, aggregate(tbodygyromeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(tbodygyromeany ~ subject + activity, aggregate(tbodygyromeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(tbodygyromeanz ~ subject + activity, aggregate(tbodygyromeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[12] <-"tbodygyromeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[13] <-"tbodygyromeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[14] <-"tbodygyromeanz"
##--------------------------------
xttmpmeanx <-xtabs(tbodygyrojerkmeanx ~ subject + activity, aggregate(tbodygyrojerkmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(tbodygyrojerkmeany ~ subject + activity, aggregate(tbodygyrojerkmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(tbodygyrojerkmeanz ~ subject + activity, aggregate(tbodygyrojerkmeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[15] <-"tbodygyrojerkmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[16] <-"tbodygyrojerkmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[17] <-"tbodygyrojerkmeanz"
##--------------------------------
xttmpmeanx <-xtabs(tbodyaccmagmean ~ subject + activity, aggregate(tbodyaccmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[18] <-"tbodyaccmagmean"
##--------------------------------
xttmpmeanx <-xtabs(tgravityaccmagmean ~ subject + activity, aggregate(tgravityaccmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[19] <-"tgravityaccmagmean"
##--------------------------------
xttmpmeanx <-xtabs(tbodyaccjerkmagmean ~ subject + activity, aggregate(tbodyaccjerkmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[20] <-"tbodyjerkaccmagmean"
##--------------------------------
xttmpmeanx <-xtabs(tbodygyromagmean ~ subject + activity, aggregate(tbodygyromagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[21] <-"tbodygyromagmean"
## frequency values
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccmeanx ~ subject + activity, aggregate(fbodyaccmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodyaccmeany ~ subject + activity, aggregate(fbodyaccmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodyaccmeanz ~ subject + activity, aggregate(fbodyaccmeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[22] <-"fbodyaccmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[23] <-"fbodyaccmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[24] <-"fbodyaccmeanz"
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccmeanfreqx ~ subject + activity, aggregate(fbodyaccmeanfreqx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodyaccmeanfreqy ~ subject + activity, aggregate(fbodyaccmeanfreqy~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodyaccmeanfreqz ~ subject + activity, aggregate(fbodyaccmeanfreqz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[25] <-"fbodyaccmeanfreqx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[26] <-"fbodyaccmeanfreqy"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[27] <-"fbodyaccmeanfreqz"
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccjerkmeanx ~ subject + activity, aggregate(fbodyaccjerkmeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodyaccjerkmeany ~ subject + activity, aggregate(fbodyaccjerkmeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodyaccjerkmeanz ~ subject + activity, aggregate(fbodyaccjerkmeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[28] <-"fbodyaccjerkmeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[29] <-"fbodyaccjerkmeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[30] <-"fbodyaccjerkmeanz"
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccjerkmeanfreqx ~ subject + activity, aggregate(fbodyaccjerkmeanfreqx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodyaccjerkmeanfreqy ~ subject + activity, aggregate(fbodyaccjerkmeanfreqy~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodyaccjerkmeanfreqz ~ subject + activity, aggregate(fbodyaccjerkmeanfreqz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[31] <-"fbodyaccjerkmeanfreqx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[32] <-"fbodyaccjerkmeanfreqy"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[33] <-"fbodyaccjerkmeanfreqz"
##--------------------------------
xttmpmeanx <-xtabs(fbodygyromeanx ~ subject + activity, aggregate(fbodygyromeanx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodygyromeany ~ subject + activity, aggregate(fbodygyromeany~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodygyromeanz ~ subject + activity, aggregate(fbodygyromeanz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[34] <-"fbodygyromeanx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[35] <-"fbodygyromeany"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[36] <-"fbodygyromeanz"
##--------------------------------
xttmpmeanx <-xtabs(fbodygyromeanfreqx ~ subject + activity, aggregate(fbodygyromeanfreqx~subject+activity, tidyset, mean))
xttmpmeany <-xtabs(fbodygyromeanfreqy ~ subject + activity, aggregate(fbodygyromeanfreqy~subject+activity, tidyset, mean))
xttmpmeanz <-xtabs(fbodygyromeanfreqz ~ subject + activity, aggregate(fbodygyromeanfreqz~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[37] <-"fbodygyromeanfreqx"
dftmp.xt <- data.frame(xttmpmeany)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[38] <-"fbodygyromeanfreqy"
dftmp.xt <-data.frame(xttmpmeanz)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[39] <-"fbodygyromeanfreqz"
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccmagmean ~ subject + activity, aggregate(fbodyaccmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[40] <-"fbodyaccmagmean"
##--------------------------------
xttmpmeanx <-xtabs(fbodyaccmagmeanfreq ~ subject + activity, aggregate(fbodyaccmagmeanfreq~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[41] <-"fbodyaccmagmeanfreq"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodyaccjerkmagmean ~ subject + activity, aggregate(fbodybodyaccjerkmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[42] <-"fbodybodyaccjerkmagmean"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodyaccjerkmagmeanfreq ~ subject + activity, aggregate(fbodybodyaccjerkmagmeanfreq~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[43] <-"fbodybodyaccjerkmagmeanfreq"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodygyromagmean ~ subject + activity, aggregate(fbodybodygyromagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[44] <-"fbodybodygyromagmean"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodygyromagmeanfreq ~ subject + activity, aggregate(fbodybodygyromagmeanfreq~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[45] <-"fbodybodygyromagmeanfreq"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodygyrojerkmagmean ~ subject + activity, aggregate(fbodybodygyrojerkmagmean~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[46] <-"fbodybodygyrojerkmagmean"
##--------------------------------
xttmpmeanx <-xtabs(fbodybodygyrojerkmagmeanfreq ~ subject + activity, aggregate(fbodybodygyrojerkmagmeanfreq~subject+activity, tidyset, mean))
dftmp.xt <- data.frame(xttmpmeanx)
newtidy <-cbind(newtidy, dftmp.xt[3])
names(newtidy)[47] <-"fbodybodygyrojerkmagmeanfreq"
##cleaning up
rm(dftmp.xt)
rm(features)
rm(subject_total)
rm(subnames)
rm(alllocs)
rm(meanlocs)
rm(stdlocs)
rm(xttmpmeanx)
rm(xttmpmeany)
rm(xttmpmeanz)
#writing the output
setwd("../")
write.table(tidyset, "tidyset.txt")
write.table(newtidy, "newtidy.txt")