-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
125 lines (96 loc) · 5.99 KB
/
run_analysis.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
## START
##
## Getting and Cleaning Data Course Project
##
## runAnalysis.r
##
## UCI HAR Dataset: downloaded from https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip
##
## This script perform the following:
## 1. Merge the training and the test sets to create one data set.
## 2. Extract only the measurements on the mean and standard deviation for each measurement.
## 3. Use descriptive activity names to name the activities in the data set
## 4. Appropriately label the data set with descriptive activity names.
## 5. From the data set in step 4, Creates a second, independent tidy data set with the average of each variable for each activity and each subject.
##
## Clear workspace
rm(list=ls())
## 1. Merge the training and the test sets to create one data set.
##set working directory to the location where the UCI HAR Dataset was unzipped
setwd('/Data/UCI HAR Dataset/');
## Read in the data from files
#import: features.txt
#import: activity_labels.txt
impfeatures = read.table('./features.txt',header=FALSE);
impactivitylabel = read.table('./activity_labels.txt',header=FALSE);
## Read in the training data set
#import: subject_train.txt
#import: x_train.txt
#import: y_train.txt
impsubjectTrain = read.table('./train/subject_train.txt',header=FALSE);
impxTrain = read.table('./train/x_train.txt',header=FALSE);
impyTrain = read.table('./train/y_train.txt',header=FALSE);
## Assigin column names
colnames(impactivitylabel) = c('activityId','activityType');
colnames(impsubjectTrain) = "subjectId";
colnames(impxTrain) = impfeatures[,2];
colnames(impyTrain) = "activityId";
## Create the final training data set by merging subjectTrain, xTrain and yTrain
trainDataSet = cbind(impyTrain,impsubjectTrain,impxTrain);
## Read in the test data set
#import: subject_test.txt
#import: x_test.txt
#import: y_test.txt
impsubjectTest = read.table('./test/subject_test.txt',header=FALSE);
impxTest = read.table('./test/x_test.txt',header=FALSE);
impyTest = read.table('./test/y_test.txt',header=FALSE);
## Assign column names to the test data
colnames(impsubjectTest) = "subjectId";
colnames(impxTest) = impfeatures[,2];
colnames(impyTest) = "activityId";
## Create the final test data set by merging the subjectTest, xTest and yTest data
testDataSet = cbind(impyTest,impsubjectTest,impxTest);
## Create the final data set : Combine training and test data
finalDataSet = rbind(trainDataSet,testDataSet);
## Create a vector for the column names from the finalDataSet
## Select the mean() & stddev() columns
colNames = colnames(finalDataSet);
## 2. Extract only the mean and standard deviation for each measurement.
## Create a logicalVector that contains TRUE values for the ID, mean() & stddev() columns and FALSE for others
logicalVector = (grepl("activity..",colNames) | grepl("subject..",colNames) | grepl("-mean..",colNames) & !grepl("-meanFreq..",colNames) & !grepl("mean..-",colNames) | grepl("-std..",colNames) & !grepl("-std()..-",colNames));
## Subset finalDataSet table based on the logicalVector to keep only desired columns
finalDataSet = finalDataSet[logicalVector==TRUE];
## 3. Use descriptive activity names to name the activities in the data set
## Merge the finalDataSet set with the acitivityType table to include descriptive activity names
finalDataSet = merge(finalDataSet,impactivitylabel,by='activityId',all.x=TRUE);
## Updating the colNames vector to include the new column names after merge
colNames = colnames(finalDataSet);
## 4. Appropriately label the data set with descriptive activity names.
## Cleaning up the variable/column names
for (i in 1:length(colNames))
{
colNames[i] = gsub("\\()","",colNames[i]) #remove \\() from the column name
colNames[i] = gsub("-mean","Mean",colNames[i]) #replace -mean with Mean
colNames[i] = gsub("-std$","StdDev",colNames[i]) #replace -std$ with StdDev
colNames[i] = gsub("^(t)","time",colNames[i]) #replace ^(t) with time
colNames[i] = gsub("^(f)","freq",colNames[i]) #replace ^(f) with freq
colNames[i] = gsub("([Gg]ravity)","Gravity",colNames[i]) #replace ([Gg]ravity) with Gravity
colNames[i] = gsub("([Bb]ody[Bb]ody|[Bb]ody)","Body",colNames[i]) #replace ([Bd]ody[Bb]ody|[Bb]ody) with Body
colNames[i] = gsub("[Gg]yro","Gyro",colNames[i]) #replace [Gg]yro with Gyro
colNames[i] = gsub("AccMag","AccMagnitude",colNames[i]) #replace AccMag with AccMagnitude
colNames[i] = gsub("([Bb]odyaccjerkmag)","BodyAccJerkMagnitude",colNames[i]) #replace ([Bb]odyaccjerkmag) with BodyAccJerkMagnitude
colNames[i] = gsub("JerkMag","JerkMagnitude",colNames[i]) #replace JerkMag with JerkMagnitude
colNames[i] = gsub("GyroMag","GyroMagnitude",colNames[i]) #replace GyroMag with GyroMagnitude
};
## Reassigning the new descriptive column names to the finalData set
colnames(finalDataSet) = colNames;
## 5. Create a second, independent clean data set with the average of each variable for each activity and each subject.
## Create a new table, finalDataNoActivityType without the activityType column
finalDataNoActivityType = finalDataSet[,names(finalDataSet) != 'activityType'];
## Summarizing the finalDataNoActivityType table to include just the mean of each variable for each activity and each subject
TidyDataSet = aggregate(finalDataNoActivityType[,names(finalDataNoActivityType) != c('activityId','subjectId')],by=list(activityId=finalDataNoActivityType$activityId,subjectId = finalDataNoActivityType$subjectId),mean);
## Merging the TidyDataSet with impactivitylabel to include descriptive acitvity names
TidyDataSet = merge(TidyDataSet,impactivitylabel,by='activityId',all.x=TRUE);
## Export the clean Data set
write.table(TidyDataSet, './tidyData.txt',row.names=TRUE,sep='\t');
## THE END