-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
109 lines (89 loc) · 4.54 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
######################
## Loading Packages ##
######################
setwd("C:/Users/Piero/Documents/Coursera/Getting and Cleaning Data Course Project")
# Function to check whether package is installed
is.installed <- function(mypkg){
is.element(mypkg, installed.packages()[,1])
}
# check if package "data.table is installed
if (!is.installed("data.table")){
install.packages("data.table")
}
# check if reshape2 package is installed
if (!"reshape2" %in% installed.packages()) {
install.packages("reshape2")
}
require(data.table)
library("reshape2")
#############################
## Data download and unzip ##
#############################
# string variables for file download
fileName <- "getdata_projectfiles_UCI HAR Dataset.zip"
url <- "http://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
dir <- "UCI HAR Dataset"
# File download verification. If file does not exist, download to working directory.
if(!file.exists(fileName)){
download.file(url,fileName, mode = "wb")
}
# File unzip verification. If the directory does not exist, unzip the downloaded file.
if(!file.exists(dir)){
unzip("UCIdata.zip", files = NULL, exdir=".")
}
###################################################################
## Merges the training and the test sets to create one data set. ##
###################################################################
x_train <- fread(input = "UCI HAR Dataset/train/X_train.txt")
subject_train <- fread(input = "UCI HAR Dataset/train/subject_train.txt")
names(subject_train)[1] <- 'subject'
label_train <- fread(input = "UCI HAR Dataset/train/y_train.txt")
names(label_train)[1] <- "activity"
x_test <- fread(input = "UCI HAR Dataset/test/X_test.txt")
subject_test <- fread(input = "UCI HAR Dataset/test/subject_test.txt")
names(subject_test)[1] <- "subject"
label_test <- fread(input = "UCI HAR Dataset/test/y_test.txt")
names(label_test)[1] <- "activity"
y_test <- read.table("UCI HAR Dataset/test/y_test.txt")
y_train <- read.table("UCI HAR Dataset/train/y_train.txt")
## merge the files
bind_train <- cbind(subject_train, label_train, x_train)
bind_test <- cbind(subject_test, label_test, x_test)
## 1. Merges the training and the test sets to create one data set.
DT <- rbind(bind_train,bind_test)
#############################################################################################
## Extracts only the measurements on the mean and standard deviation for each measurement. ##
#############################################################################################
## Create a column vector of mean
means <- apply(DT[3:563], 2, mean)
## Create a colum vector of standard deviation
stand_deviation <- apply(DT[3:563], 2, sd)
## Combine both results
extracted_stats <- cbind(means,stand_deviation)
############################################################################
## Uses descriptive activity names to name the activities in the data set ##
############################################################################
activity_labels <- read.table("UCI HAR Dataset/activity_labels.txt")
act_group <- factor(DT$activity)
levels(act_group) <- activity_labels[,2]
DT$activity <- act_group
########################################################################
## Appropriately labels the data set with descriptive variable names. ##
########################################################################
features <- fread(input = "UCI HAR Dataset/features.txt")
# Create vector of "Clean" feature names by getting rid of "()" apply to the dataSet to rename labels.
CleanFeatureNames <- sapply(features[, 2], function(x) {gsub("[()]", "",x)})
names(DT) <- CleanFeatureNames[means]
# combine test and train of subject data and activity data, give descriptive lables
subject <- rbind(subject_train, subject_test)
names(subject) <- 'subject'
activity <- rbind(y_train, y_test)
names(activity) <- 'activity'
# combine subject, activity, and mean and std only data set to create final data set.
DT <- cbind(subject,activity, DT)
####################################################################################################################################################
## 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. ##
####################################################################################################################################################
secondDataSet <- aggregate( . ~ subject + activity, data = DT, FUN = mean )
# write out tidy Data
write.table( secondDataSet, "tidy_data.txt", row.names = FALSE )