-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81d8607
commit 4681f7b
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Download the file and extract it to your R working Directory | ||
# https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip | ||
# read data set X | ||
training <- read.table("UCI HAR Dataset/train/X_train.txt") | ||
test <- read.table("UCI HAR Dataset/test/X_test.txt") | ||
# merge Data set X | ||
XCombined <- rbind(training, test) | ||
|
||
# read data set subject | ||
training <- read.table("UCI HAR Dataset/train/subject_train.txt") | ||
test <- read.table("UCI HAR Dataset/test/subject_test.txt") | ||
# merge data set X | ||
SCombined <- rbind(training, test ) | ||
|
||
# read data set Y | ||
training <- read.table("UCI HAR Dataset/train/y_train.txt") | ||
test <- read.table("UCI HAR Dataset/test/y_test.txt") | ||
# merge data set Y | ||
YCombined <- rbind(training, test) | ||
|
||
# Extract mean and standard deviation for each measurement | ||
features <- read.table("UCI HAR Dataset/features.txt") | ||
colsNeeded <- grep(".*mean\\(\\)|.*std\\(\\)", features$MeasureName) | ||
XCombined <- XCombined[,colsNeeded] | ||
|
||
#Add descriptive activity names to name the activityLabels in the data set | ||
names(YCombined) <- "activity" | ||
activityLabels <- read.table("UCI HAR Dataset/activity_labels.txt") | ||
activityLabels[, 2] = gsub("_", "", tolower(as.character(activityLabels[, 2]))) | ||
YCombined[,1] = activityLabels[YCombined[,1], 2] | ||
|
||
names(SCombined) <- "subject" | ||
merged <- cbind(SCombined, YCombined, XCombined) | ||
write.table(merged, "merged-data.txt") | ||
|
||
# make averages data set | ||
|
||
uniqueSubjects <- unique(SCombined)[,1] | ||
numCols <- dim(merged)[2] | ||
result <- merged[1:(length(unique(SCombined)[,1])*length(activityLabels[,1])), ] | ||
|
||
row = 1 | ||
for (sub in 1:length(unique(SCombined)[,1])) { | ||
for (act in 1:length(activityLabels[,1])) { | ||
result[row, 1] = uniqueSubjects[sub] | ||
result[row, 2] = activityLabels[act, 2] | ||
tmp <- merged[merged$subject==sub & merged$activity==activityLabels[act, 2], ] | ||
result[row, 3:numCols] <- colMeans(tmp[, 3:numCols]) | ||
row = row+1 | ||
} | ||
} | ||
write.table(result, "averages.txt") |