-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
86 lines (69 loc) · 4.17 KB
/
run_analysis.R
File metadata and controls
86 lines (69 loc) · 4.17 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
## Coursera Getting and Cleaning Data Course Project
## Jianglin Huang
## 2016-03-15
rm(list=ls())
# set the working destination
#setwd("C:/Users/jianhuang7/Dropbox/WorkStation/PhD/Course_Materials/Coursera/R/UCI HAR Dataset")
setwd("/Users/Hellwalker_Mac/Dropbox/WorkStation/PhD/Course_Materials/Coursera/R/UCI HAR Dataset")
#imports features.txt
fs = read.table('./features.txt',header=F)
#imports activity_labels.txt
activityType = read.table('./activity_labels.txt',header=FALSE)
subjectTrain = read.table('./train/subject_train.txt',header=FALSE)#imports subject_train.txt
xTrain = read.table('./train/x_train.txt',header=FALSE)#imports x_train.txt
yTrain = read.table('./train/y_train.txt',header=FALSE) #imports y_train.txt
# 1Merges the training and the test sets to create one data set.
# Assigin column names to the data imported above
colnames(activityType) = c('activityId','activityType');
colnames(subjectTrain) = "subjectId";
colnames(xTrain) = fs[,2];
colnames(yTrain) = "activityId";
# cCreate the final training set by merging yTrain, subjectTrain, and xTrain
trainingData = cbind(yTrain,subjectTrain,xTrain);
# Read in the test data
subjectTest = read.table('./test/subject_test.txt',header=FALSE); #imports subject_test.txt
xTest = read.table('./test/x_test.txt',header=FALSE); #imports x_test.txt
yTest = read.table('./test/y_test.txt',header=FALSE); #imports y_test.txt
# Assign column names to the test data imported above
colnames(subjectTest) = "subjectId";
colnames(xTest) = fs[,2];
colnames(yTest) = "activityId";
# Create the final test set by merging the xTest, yTest and subjectTest data
testData = cbind(yTest,subjectTest,xTest);
# Combine training and test data to create a final data set
finalData = rbind(trainingData,testData);
# to select the desired mean() & stddev() columns
colNames = colnames(finalData);
# 2. Extract only the measurements on the mean and standard deviation for each measurement.
logicalVector = (grepl("activity..",colNames) | grepl("subject..",colNames) | grepl("-mean..",colNames) & !grepl("-meanFreq..",colNames) & !grepl("mean..-",colNames) | grepl("-std..",colNames) & !grepl("-std()..-",colNames));
# Subset finalData table based on the logicalVector to keep only desired columns
finalData = finalData[logicalVector==TRUE];
# 3. Use descriptive activity names to name the activities in the data set
# Merge the finalData set with the acitivityType table to include descriptive activity names
finalData = merge(finalData,activityType,by='activityId',all.x=TRUE);
# Updating the colNames vector to include the new column names after merge
colNames = colnames(finalData);
# 4. Appropriately label the data set with descriptive activity names.
for (i in 1:length(colNames))
{
colNames[i] = gsub("\\()","",colNames[i])
colNames[i] = gsub("-std$","StdDev",colNames[i])
colNames[i] = gsub("-mean","Mean",colNames[i])
colNames[i] = gsub("^(t)","time",colNames[i])
colNames[i] = gsub("^(f)","freq",colNames[i])
colNames[i] = gsub("([Gg]ravity)","Gravity",colNames[i])
colNames[i] = gsub("([Bb]ody[Bb]ody|[Bb]ody)","Body",colNames[i])
colNames[i] = gsub("[Gg]yro","Gyro",colNames[i])
colNames[i] = gsub("AccMag","AccMagnitude",colNames[i])
colNames[i] = gsub("([Bb]odyaccjerkmag)","BodyAccJerkMagnitude",colNames[i])
colNames[i] = gsub("JerkMag","JerkMagnitude",colNames[i])
colNames[i] = gsub("GyroMag","GyroMagnitude",colNames[i])
};
# Reassigning the new descriptive column names to the finalData set
colnames(finalData) = colNames;
# 5. Create a second, independent tidy data set with the average of each variable for each activity and each subject.
finalDataNoActivityType = finalData[,names(finalData) != 'activityType'];
# Summarizing the finalDataNoActivityType table to include just the mean of each variable for each activity and each subject
tidyData = aggregate(finalDataNoActivityType[,names(finalDataNoActivityType) != c('activityId','subjectId')],by=list(activityId=finalDataNoActivityType$activityId,subjectId = finalDataNoActivityType$subjectId),mean);
tidyData = merge(tidyData,activityType,by='activityId',all.x=TRUE);
write.table(tidyData, './tidyData.txt',row.names = F,sep='\t')