-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
51 lines (38 loc) · 1.97 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
# Sorting three sets of data, training, testing, activityLabels
training = read.csv("UCI HAR Dataset/train/X_train.txt", sep="", header=FALSE)
training[,562] = read.csv("UCI HAR Dataset/train/Y_train.txt", sep="", header=FALSE)
training[,563] = read.csv("UCI HAR Dataset/train/subject_train.txt", sep="", header=FALSE)
testing = read.csv("UCI HAR Dataset/test/X_test.txt", sep="", header=FALSE)
testing[,562] = read.csv("UCI HAR Dataset/test/Y_test.txt", sep="", header=FALSE)
testing[,563] = read.csv("UCI HAR Dataset/test/subject_test.txt", sep="", header=FALSE)
activityLabels = read.csv("UCI HAR Dataset/activity_labels.txt", sep="", header=FALSE)
# Read features and make friendly for R
features = read.csv("UCI HAR Dataset/features.txt", sep="", header=FALSE)
features[,2] = gsub('-mean', 'Mean', features[,2])
features[,2] = gsub('-std', 'Std', features[,2])
features[,2] = gsub('[-()]', '', features[,2])
# Merge the training and test sets together
allData = rbind(training, testing)
# Get only the data on mean and std dev
colsWeWant <- grep(".*Mean.*|.*Std.*", features[,2])
# Reduce the features table to what we want
features <- features[colsWeWant,]
# Add last two columns (subject and activity)
colsWeWant <- c(colsWeWant, 562, 563)
# Remove the unwanted columns from allData
allData <- allData[,colsWeWant]
# Add the column names (features) to allData
colnames(allData) <- c(features$V2, "Activity", "Subject")
colnames(allData) <- tolower(colnames(allData))
currentActivity = 1
for (currentActivityLabel in activityLabels$V2) {
allData$activity <- gsub(currentActivity, currentActivityLabel, allData$activity)
currentActivity <- currentActivity + 1
}
allData$activity <- as.factor(allData$activity)
allData$subject <- as.factor(allData$subject)
tidy = aggregate(allData, by=list(activity = allData$activity, subject=allData$subject), mean)
# Remove subject and activity column, since their mean is not needed
tidy[,90] = NULL
tidy[,89] = NULL
write.table(tidy, "tidy.txt", sep="\t")