<repo_name>minollisantiago/Getting-and-Cleaning-Data-Peer-Assessment<file_sep>/CodeBook.md
#Code Book - Peer Assessment

###General Description
This dataset is based on the "Human Activity Recognition Using Smartphones Dataset - Version 1.0[1]", which
can be found here: 

http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones 

For all the features included in the original dataset, only the mean and standard deviation measurements
were considered. Both the train set and test set were merged into one dataset and Activity and Subject 
labels were included. 

The dataset is composed by 180 observations (rows) for 68 variables (columns), where each row is a vector
that contains the average value of the mean and standard deviation measurements from the original dataset
for each activity and subject. 

The original dataset included 30 subjects and 6 activity labels, thus the number of observations of this 
dataset is equal to 30*6 = 180. The original dataset also included a total of 66 mean and standard deviation
measurements, the 2 additional columns included in this dataset are the Subject and Activity labels.

###Variable Description
More information about the original dataset, including the experiment's description, and a more detailed 
description of the features can be found in the link that was provided in the general description of this
dataset.

The features selected for the original dataset come from the accelerometer and gyroscope 3-axial raw signals 
tAcc-XYZ and tGyro-XYZ. These time domain signals (prefix 't' to denote time) were captured at a constant rate 
of 50 Hz. Then they were filtered using a median filter and a 3rd order low pass Butterworth filter with a corner 
frequency of 20 Hz to remove noise. Similarly, the acceleration signal was then separated into body and gravity 
acceleration signals (tBodyAcc-XYZ and tGravityAcc-XYZ) using another low pass Butterworth filter with a corner 
frequency of 0.3 Hz. 

Subsequently, the body linear acceleration and angular velocity were derived in time to obtain Jerk signals 
(tBodyAccJerk-XYZ and tBodyGyroJerk-XYZ). Also the magnitude of these three-dimensional signals were calculated 
using the Euclidean norm (tBodyAccMag, tGravityAccMag, tBodyAccJerkMag, tBodyGyroMag, tBodyGyroJerkMag). 

Finally a Fast Fourier Transform (FFT) was applied to some of these signals producing fBodyAcc-XYZ, fBodyAccJerk-XYZ, 
fBodyGyro-XYZ, fBodyAccJerkMag, fBodyGyroMag, fBodyGyroJerkMag. (Note the 'f' to indicate frequency domain signals). 

These signals were used to estimate variables of the feature vector for each pattern:  
'-XYZ' is used to denote 3-axial signals in the X, Y and Z directions.

* tBodyAcc-XYZ
* tGravityAcc-XYZ
* tBodyAccJerk-XYZ
* tBodyGyro-XYZ
* tBodyGyroJerk-XYZ
* tBodyAccMag
* tGravityAccMag
* tBodyAccJerkMag
* tBodyGyroMag
* tBodyGyroJerkMag
* fBodyAcc-XYZ
* fBodyAccJerk-XYZ
* fBodyGyro-XYZ
* fBodyAccMag
* fBodyAccJerkMag
* fBodyGyroMag
* fBodyGyroJerkMag

Several variables were estimated from these signals, but the only ones that were included in this dataset are the
following measurements: 

* mean(): Mean value
* std(): Standard deviation

The original experiments were carried out with a group of 30 volunteers within an age bracket of 19-48 years. 
Each person performed six activities (WALKING, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, LAYING) wearing 
a smartphone (Samsung Galaxy S II) on the waist.

The measurements described above were averaged by each subject and activity. Two additional variables were included 
in the dataset, the first one (Subject) identifies the subject and the second one (Activity) identifies the activity.

An ordered list of all the dataset's variables is included below:  

1. Subject 
2. Activity 
3. tBodyAcc-mean()-X 
4. tBodyAcc-mean()-Y 
5. tBodyAcc-mean()-Z 
6. tBodyAcc-std()-X 
7. tBodyAcc-std()-Y 
8. tBodyAcc-std()-Z 
9. tGravityAcc-mean()-X 
10. tGravityAcc-mean()-Y 
11. tGravityAcc-mean()-Z 
12. tGravityAcc-std()-X 
13. tGravityAcc-std()-Y 
14. tGravityAcc-std()-Z 
15. tBodyAccJerk-mean()-X 
16. tBodyAccJerk-mean()-Y 
17. tBodyAccJerk-mean()-Z 
18. tBodyAccJerk-std()-X 
19. tBodyAccJerk-std()-Y 
20. tBodyAccJerk-std()-Z 
21. tBodyGyro-mean()-X 
22. tBodyGyro-mean()-Y 
23. tBodyGyro-mean()-Z 
24. tBodyGyro-std()-X 
25. tBodyGyro-std()-Y 
26. tBodyGyro-std()-Z 
27. tBodyGyroJerk-mean()-X 
28. tBodyGyroJerk-mean()-Y 
29. tBodyGyroJerk-mean()-Z 
30. tBodyGyroJerk-std()-X 
31. tBodyGyroJerk-std()-Y 
32. tBodyGyroJerk-std()-Z 
33. tBodyAccMag-mean() 
34. tBodyAccMag-std() 
35. tGravityAccMag-mean() 
36. tGravityAccMag-std() 
37. tBodyAccJerkMag-mean() 
38. tBodyAccJerkMag-std() 
39. tBodyGyroMag-mean() 
40. tBodyGyroMag-std() 
41. tBodyGyroJerkMag-mean() 
42. tBodyGyroJerkMag-std() 
43. fBodyAcc-mean()-X 
44. fBodyAcc-mean()-Y 
45. fBodyAcc-mean()-Z 
46. fBodyAcc-std()-X 
47. fBodyAcc-std()-Y 
48. fBodyAcc-std()-Z 
49. fBodyAccJerk-mean()-X 
50. fBodyAccJerk-mean()-Y 
51. fBodyAccJerk-mean()-Z 
52. fBodyAccJerk-std()-X 
53. fBodyAccJerk-std()-Y 
54. fBodyAccJerk-std()-Z 
55. fBodyGyro-mean()-X 
56. fBodyGyro-mean()-Y 
57. fBodyGyro-mean()-Z 
58. fBodyGyro-std()-X 
59. fBodyGyro-std()-Y 
60. fBodyGyro-std()-Z 
61. fBodyAccMag-mean() 
62. fBodyAccMag-std() 
63. fBodyBodyAccJerkMag-mean() 
64. fBodyBodyAccJerkMag-std() 
65. fBodyBodyGyroMag-mean() 
66. fBodyBodyGyroMag-std() 
67. fBodyBodyGyroJerkMag-mean() 
68. fBodyBodyGyroJerkMag-std()

###Notes: 
* Original features are normalized and bounded within [-1,1].
* Each row in the dataset is a vector that contains the average mean and standard
   deviation measurements from the original data set for each subject and activity.

###References
[1] <NAME>, <NAME>, <NAME>, <NAME> and <NAME>. 
Human Activity Recognition on Smartphones using a Multiclass Hardware-Friendly Support 
Vector Machine. International Workshop of Ambient Assisted Living (IWAAL 2012). 
Vitoria-Gasteiz, Spain. Dec 2012<file_sep>/README.md
#Peer Assessment

###Important note
I have included two .R files, one called run_analysis.R and the other run_analysis2.R.
The difference between both files is the way in which they get the Samsung data, the 
first one doesn't require you to have the data files on your working directory, it will
handle the downloading in a temp file and delete it after unzipping and reading the data 
sets into memory. The second one works as required by the assignment, it will get the 
data sets from the working directory (after you decompress the data that is).

###How the script works
Both scripts are split into steps that mimic the ones given in the assignment description, 
they also include annotations that describe the whole process step by step. 

Here is a general overview of the steps:

* Step 0: Loads the necessary data sets into memory.
* Steps 1, 3 and 4: These steps are together, the script handles the merging of the 
   data sets and adds the labels for Subjects and Activities.
* Step 2: Subsets the data, including only the mean and std measurements. 
* Step 5: Creates the final tidy data set and writes it into a .txt file in the working 
   directory. This step includes the computation of the average of the selected measurements 
   for each activity and each subject.
 
###Tidy Data Set Notes
The tidy dataset provided for the assignment is also included in this repo, it is a .txt 
file with the following particularities:

* Row names were not included in the dataset, this makes it easier to write/read the file.
* Column names are included in the file, to identify the variables.
* Values were separated using the comma (",").
  
###A final note on reading the tidy dataset
If you load the TidyDataSet.txt into memory, remember to set the arguments header to TRUE 
and sep to "," when you call the read.table() function, here is an example:

```{r}
read.table("TidyDataSet.txt", sep = ",", header = TRUE)
```
  <file_sep>/run_analysis2.R

##STEP 0:
#load the needed files into globalenv()

#Train set files
TrainSet <- read.table("UCI HAR Dataset/train/X_train.txt")
TrainSetLab <- read.table("UCI HAR Dataset/train/y_train.txt")
TrainSetSubj <- read.table("UCI HAR Dataset/train/subject_train.txt")

#Test set files
TestSet <- read.table("UCI HAR Dataset/test/X_test.txt")
TestSetLab <- read.table("UCI HAR Dataset/test/y_test.txt")
TestSetSubj <- read.table("UCI HAR Dataset/test/subject_test.txt")

#Labels
ActivityLabels <- read.table("UCI HAR Dataset/activity_labels.txt")
Features <- read.table("UCI HAR Dataset/features.txt")


##STEPS 1, 3 AND 4:
#Merge data sets (TrainSet and TestSet) 
#Add the Subject and Activity labels (descriptive names) to the data

#1) Adding Subject and Activity labels to data

#TrainSet
TrainSet <- data.frame(Subject = TrainSetSubj$V1, 
Activity = factor(TrainSetLab$V1, levels = 1:6, 
labels = as.character(ActivityLabels$V2)), TrainSet)

#TestSet
TestSet <- data.frame(Subject = TestSetSubj$V1, 
Activity = factor(TestSetLab$V1, levels = 1:6, 
labels = as.character(ActivityLabels$V2)), TestSet)

#2) Merge both sets

Data <- rbind(TrainSet, TestSet)


##STEP 2:
#Subset columns (only mean and std measurements)

#1) Original variable names

names(Data)[-(1:2)] <- as.character(Features$V2)

#2) Find the needed measurements (labels), eliminate the rest 

#Feature labels with the words "mean" and "std" on them
x <- grep(paste(c("mean", "std"), collapse = "|"), 
names(Data)[-(1:2)], ignore.case = TRUE, value = TRUE)

#Feature labels with the words "meanFreq" and "angle" on them 
y <- grep(paste(c("meanFreq", "angle"), collapse = "|"), 
names(Data)[-(1:2)], ignore.case = TRUE, value = TRUE)

#Final filtered labels, only mean and std measurements
FilteredFeatureLabels <- x[!x %in% y]

#3) Subsetting the dataset (only mean and std measurements)

Data <- Data[, c(names(Data)[1:2], FilteredFeatureLabels)]


##STEP 5:
#New tidy data set

#1) Data as data.table, for easier subsetting

#Install and load the package data.table (checks to see if its installed first)
if( !require(data.table, quietly = TRUE) ) { install.packages("data.table") } 
require(data.table)

#New set as data.table()
TidyData <- data.table(Data)

#2) Set the keys, and calculate the means by Subject and activities

#Set the keys for the data table (Subject and Activity)
setkey(TidyData, Subject, Activity)

#Subset and compute the mean
TidyData <- TidyData[, lapply(.SD, mean), by ="Subject,Activity"]

#3) Write the tidy data set to a file (in the working directory)

write.table(TidyData, file = "TidyDataSet.txt", row.names = FALSE, 
col.names = TRUE, sep = ",", quote = FALSE)
