Course Project: Getting and Cleaning Data

School and Instructor: John Hopkins, Bloomberg School of Public Health, Jeff Leek, PHD
Student Email: al_shain@me.com

Description:

The purpose of this project is to demonstrate your ability to collect, work with, and clean a data set. The goal is to prepare tidy data that can be used for later analysis. You will be graded by your peers on a series of yes/no questions related to the project. You will be required to submit: 1) a tidy data set as described below, 2) a link to a Github repository with your script for performing the analysis, and 3) a code book that describes the variables, the data, and any transformations or work that you performed to clean up the data called CodeBook.md. You should also include a README.md in the repo with your scripts. This repo explains how all of the scripts work and how they are connected.

One of the most exciting areas in all of data science right now is wearable computing - see for example this article . Companies like Fitbit, Nike, and Jawbone Up are racing to develop the most advanced algorithms to attract new users. The data linked to from the course website represent data collected from the accelerometers from the Samsung Galaxy S smartphone. A full description is available at the site where the data was obtained:

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

Here are the data for the project:

https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip


Step 1. set workding directory to project

setwd("~/Desktop/Coursera/GettingAndCleansingData/Project")

Step 2. make sure the library “reshape2” is loaded

library(reshape2)

Step 3. set the download, URL, and unzip file name

downloadFile <- "data/getdata_dataset.zip"

Step 4. download and unzip the filename

downloadURL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"

Step 5. set variables for URL, file and download locations

trainXFile <- "./data/UCI HAR Dataset/train/X_train.txt"
trainLabels <- "./data/UCI HAR Dataset/train/y_train.txt"
trainSubjectFile <- ".data/UCI HAR Dataset/train/subject_train.txt"
testXFile <- "./data/UCI HAR Dataset/test/X_test.txt"
testLabels <- "./data/UCI HAR Dataset/test/y_test.txt"
testSubjectFile <- ".data/UCI HAR Dataset/test/subject_test.txt"

Step 6. test for data foloder and zip file, if NOT found create

if(!file.exists("./data")) { dir.create("./data")}
if (!file.exists(downloadFile)) {
  download.file(downloadURL, downloadFile, method = "curl");
  unzip(downloadFile, overwrite = T, exdir = ".")
}

Step 7. Load activity labels - Uses descriptive activity names to name the activities in the data set

activityLabels <- read.table("./data/UCI HAR Dataset/activity_labels.txt")
activityLabels[,2] <- as.character(activityLabels[,2])
features <- read.table("./data/UCI HAR Dataset/features.txt")
features[,2] <- as.character(features[,2])

Step 8. Extract only the data on mean and standard deviation

featuresWanted <- grep(".*mean.*|.*std.*", features[,2])
featuresWanted.names <- features[featuresWanted,2]
featuresWanted.names = gsub('-mean', 'Mean', featuresWanted.names)
featuresWanted.names = gsub('-std', 'Std', featuresWanted.names)
featuresWanted.names <- gsub('[-()]', '', featuresWanted.names)

Step 9. Load the data sets

train <- read.table("./data/UCI HAR Dataset/train/X_train.txt")[featuresWanted]
trainActivities <- read.table("./data/UCI HAR Dataset/train/Y_train.txt")
trainSubjects <- read.table("./data/UCI HAR Dataset/train/subject_train.txt")
train <- cbind(trainSubjects, trainActivities, train)
test <- read.table("./data/UCI HAR Dataset/test/X_test.txt")[featuresWanted]
testActivities <- read.table("./data/UCI HAR Dataset/test/Y_test.txt")
testSubjects <- read.table("./data/UCI HAR Dataset/test/subject_test.txt")
test <- cbind(testSubjects, testActivities, test)

Step 10. Merge train and test data sets and add thier labels

combinedData <- rbind(train, test)
colnames(combinedData) <- c("subject", "activity", featuresWanted.names)

Step 11. Turn activities & subjects into factors

combinedData$activity <- factor(combinedData$activity, levels = activityLabels[,1], labels = activityLabels[,2])
combinedData$subject <- as.factor(combinedData$subject)
combinedData.melted <- melt(combinedData, id = c("subject", "activity"))
combinedData.mean <- dcast(combinedData.melted, subject + activity ~ variable, mean)

Step 12. Write out the tidy data set

write.table(combinedData.mean, "tidy.txt", row.names=FALSE, quote=FALSE, sep="\t")

Explore Data

Dataset structure

dtTidy=read.csv("Tidy.txt",sep="\t",header=TRUE )
str(dtTidy)
## 'data.frame':    180 obs. of  81 variables:
##  $ subject                     : int  1 1 1 1 1 1 2 2 2 2 ...
##  $ activity                    : Factor w/ 6 levels "LAYING","SITTING",..: 4 6 5 2 3 1 4 6 5 2 ...
##  $ tBodyAccMeanX               : num  0.277 0.255 0.289 0.261 0.279 ...
##  $ tBodyAccMeanY               : num  -0.01738 -0.02395 -0.00992 -0.00131 -0.01614 ...
##  $ tBodyAccMeanZ               : num  -0.1111 -0.0973 -0.1076 -0.1045 -0.1106 ...
##  $ tBodyAccStdX                : num  -0.284 -0.355 0.03 -0.977 -0.996 ...
##  $ tBodyAccStdY                : num  0.11446 -0.00232 -0.03194 -0.92262 -0.97319 ...
##  $ tBodyAccStdZ                : num  -0.26 -0.0195 -0.2304 -0.9396 -0.9798 ...
##  $ tGravityAccMeanX            : num  0.935 0.893 0.932 0.832 0.943 ...
##  $ tGravityAccMeanY            : num  -0.282 -0.362 -0.267 0.204 -0.273 ...
##  $ tGravityAccMeanZ            : num  -0.0681 -0.0754 -0.0621 0.332 0.0135 ...
##  $ tGravityAccStdX             : num  -0.977 -0.956 -0.951 -0.968 -0.994 ...
##  $ tGravityAccStdY             : num  -0.971 -0.953 -0.937 -0.936 -0.981 ...
##  $ tGravityAccStdZ             : num  -0.948 -0.912 -0.896 -0.949 -0.976 ...
##  $ tBodyAccJerkMeanX           : num  0.074 0.1014 0.0542 0.0775 0.0754 ...
##  $ tBodyAccJerkMeanY           : num  0.028272 0.019486 0.02965 -0.000619 0.007976 ...
##  $ tBodyAccJerkMeanZ           : num  -0.00417 -0.04556 -0.01097 -0.00337 -0.00369 ...
##  $ tBodyAccJerkStdX            : num  -0.1136 -0.4468 -0.0123 -0.9864 -0.9946 ...
##  $ tBodyAccJerkStdY            : num  0.067 -0.378 -0.102 -0.981 -0.986 ...
##  $ tBodyAccJerkStdZ            : num  -0.503 -0.707 -0.346 -0.988 -0.992 ...
##  $ tBodyGyroMeanX              : num  -0.0418 0.0505 -0.0351 -0.0454 -0.024 ...
##  $ tBodyGyroMeanY              : num  -0.0695 -0.1662 -0.0909 -0.0919 -0.0594 ...
##  $ tBodyGyroMeanZ              : num  0.0849 0.0584 0.0901 0.0629 0.0748 ...
##  $ tBodyGyroStdX               : num  -0.474 -0.545 -0.458 -0.977 -0.987 ...
##  $ tBodyGyroStdY               : num  -0.05461 0.00411 -0.12635 -0.96647 -0.98773 ...
##  $ tBodyGyroStdZ               : num  -0.344 -0.507 -0.125 -0.941 -0.981 ...
##  $ tBodyGyroJerkMeanX          : num  -0.09 -0.1222 -0.074 -0.0937 -0.0996 ...
##  $ tBodyGyroJerkMeanY          : num  -0.0398 -0.0421 -0.044 -0.0402 -0.0441 ...
##  $ tBodyGyroJerkMeanZ          : num  -0.0461 -0.0407 -0.027 -0.0467 -0.049 ...
##  $ tBodyGyroJerkStdX           : num  -0.207 -0.615 -0.487 -0.992 -0.993 ...
##  $ tBodyGyroJerkStdY           : num  -0.304 -0.602 -0.239 -0.99 -0.995 ...
##  $ tBodyGyroJerkStdZ           : num  -0.404 -0.606 -0.269 -0.988 -0.992 ...
##  $ tBodyAccMagMean             : num  -0.137 -0.1299 0.0272 -0.9485 -0.9843 ...
##  $ tBodyAccMagStd              : num  -0.2197 -0.325 0.0199 -0.9271 -0.9819 ...
##  $ tGravityAccMagMean          : num  -0.137 -0.1299 0.0272 -0.9485 -0.9843 ...
##  $ tGravityAccMagStd           : num  -0.2197 -0.325 0.0199 -0.9271 -0.9819 ...
##  $ tBodyAccJerkMagMean         : num  -0.1414 -0.4665 -0.0894 -0.9874 -0.9924 ...
##  $ tBodyAccJerkMagStd          : num  -0.0745 -0.479 -0.0258 -0.9841 -0.9931 ...
##  $ tBodyGyroMagMean            : num  -0.161 -0.1267 -0.0757 -0.9309 -0.9765 ...
##  $ tBodyGyroMagStd             : num  -0.187 -0.149 -0.226 -0.935 -0.979 ...
##  $ tBodyGyroJerkMagMean        : num  -0.299 -0.595 -0.295 -0.992 -0.995 ...
##  $ tBodyGyroJerkMagStd         : num  -0.325 -0.649 -0.307 -0.988 -0.995 ...
##  $ fBodyAccMeanX               : num  -0.2028 -0.4043 0.0382 -0.9796 -0.9952 ...
##  $ fBodyAccMeanY               : num  0.08971 -0.19098 0.00155 -0.94408 -0.97707 ...
##  $ fBodyAccMeanZ               : num  -0.332 -0.433 -0.226 -0.959 -0.985 ...
##  $ fBodyAccStdX                : num  -0.3191 -0.3374 0.0243 -0.9764 -0.996 ...
##  $ fBodyAccStdY                : num  0.056 0.0218 -0.113 -0.9173 -0.9723 ...
##  $ fBodyAccStdZ                : num  -0.28 0.086 -0.298 -0.934 -0.978 ...
##  $ fBodyAccMeanFreqX           : num  -0.2075 -0.4187 -0.3074 -0.0495 0.0865 ...
##  $ fBodyAccMeanFreqY           : num  0.1131 -0.1607 0.0632 0.0759 0.1175 ...
##  $ fBodyAccMeanFreqZ           : num  0.0497 -0.5201 0.2943 0.2388 0.2449 ...
##  $ fBodyAccJerkMeanX           : num  -0.1705 -0.4799 -0.0277 -0.9866 -0.9946 ...
##  $ fBodyAccJerkMeanY           : num  -0.0352 -0.4134 -0.1287 -0.9816 -0.9854 ...
##  $ fBodyAccJerkMeanZ           : num  -0.469 -0.685 -0.288 -0.986 -0.991 ...
##  $ fBodyAccJerkStdX            : num  -0.1336 -0.4619 -0.0863 -0.9875 -0.9951 ...
##  $ fBodyAccJerkStdY            : num  0.107 -0.382 -0.135 -0.983 -0.987 ...
##  $ fBodyAccJerkStdZ            : num  -0.535 -0.726 -0.402 -0.988 -0.992 ...
##  $ fBodyAccJerkMeanFreqX       : num  -0.209 -0.377 -0.253 0.257 0.314 ...
##  $ fBodyAccJerkMeanFreqY       : num  -0.3862 -0.5095 -0.3376 0.0475 0.0392 ...
##  $ fBodyAccJerkMeanFreqZ       : num  -0.18553 -0.5511 0.00937 0.09239 0.13858 ...
##  $ fBodyGyroMeanX              : num  -0.339 -0.493 -0.352 -0.976 -0.986 ...
##  $ fBodyGyroMeanY              : num  -0.1031 -0.3195 -0.0557 -0.9758 -0.989 ...
##  $ fBodyGyroMeanZ              : num  -0.2559 -0.4536 -0.0319 -0.9513 -0.9808 ...
##  $ fBodyGyroStdX               : num  -0.517 -0.566 -0.495 -0.978 -0.987 ...
##  $ fBodyGyroStdY               : num  -0.0335 0.1515 -0.1814 -0.9623 -0.9871 ...
##  $ fBodyGyroStdZ               : num  -0.437 -0.572 -0.238 -0.944 -0.982 ...
##  $ fBodyGyroMeanFreqX          : num  0.0148 -0.1875 -0.1005 0.1892 -0.1203 ...
##  $ fBodyGyroMeanFreqY          : num  -0.0658 -0.4736 0.0826 0.0631 -0.0447 ...
##  $ fBodyGyroMeanFreqZ          : num  0.000773 -0.133374 -0.075676 -0.029784 0.100608 ...
##  $ fBodyAccMagMean             : num  -0.1286 -0.3524 0.0966 -0.9478 -0.9854 ...
##  $ fBodyAccMagStd              : num  -0.398 -0.416 -0.187 -0.928 -0.982 ...
##  $ fBodyAccMagMeanFreq         : num  0.1906 -0.0977 0.1192 0.2367 0.2846 ...
##  $ fBodyBodyAccJerkMagMean     : num  -0.0571 -0.4427 0.0262 -0.9853 -0.9925 ...
##  $ fBodyBodyAccJerkMagStd      : num  -0.103 -0.533 -0.104 -0.982 -0.993 ...
##  $ fBodyBodyAccJerkMagMeanFreq : num  0.0938 0.0854 0.0765 0.3519 0.4222 ...
##  $ fBodyBodyGyroMagMean        : num  -0.199 -0.326 -0.186 -0.958 -0.985 ...
##  $ fBodyBodyGyroMagStd         : num  -0.321 -0.183 -0.398 -0.932 -0.978 ...
##  $ fBodyBodyGyroMagMeanFreq    : num  0.268844 -0.219303 0.349614 -0.000262 -0.028606 ...
##  $ fBodyBodyGyroJerkMagMean    : num  -0.319 -0.635 -0.282 -0.99 -0.995 ...
##  $ fBodyBodyGyroJerkMagStd     : num  -0.382 -0.694 -0.392 -0.987 -0.995 ...
##  $ fBodyBodyGyroJerkMagMeanFreq: num  0.191 0.114 0.19 0.185 0.334 ...

Show a few rows of the dataset

head(dtTidy,2)
##   subject         activity tBodyAccMeanX tBodyAccMeanY tBodyAccMeanZ
## 1       1          WALKING     0.2773308   -0.01738382    -0.1111481
## 2       1 WALKING_UPSTAIRS     0.2554617   -0.02395315    -0.0973020
##   tBodyAccStdX tBodyAccStdY tBodyAccStdZ tGravityAccMeanX tGravityAccMeanY
## 1   -0.2837403  0.114461337  -0.26002790        0.9352232       -0.2821650
## 2   -0.3547080 -0.002320265  -0.01947924        0.8933511       -0.3621534
##   tGravityAccMeanZ tGravityAccStdX tGravityAccStdY tGravityAccStdZ
## 1      -0.06810286      -0.9766096      -0.9713060      -0.9477172
## 2      -0.07540294      -0.9563670      -0.9528492      -0.9123794
##   tBodyAccJerkMeanX tBodyAccJerkMeanY tBodyAccJerkMeanZ tBodyAccJerkStdX
## 1        0.07404163        0.02827211      -0.004168406       -0.1136156
## 2        0.10137273        0.01948631      -0.045562545       -0.4468439
##   tBodyAccJerkStdY tBodyAccJerkStdZ tBodyGyroMeanX tBodyGyroMeanY
## 1        0.0670025       -0.5026998    -0.04183096    -0.06953005
## 2       -0.3782744       -0.7065935     0.05054938    -0.16617002
##   tBodyGyroMeanZ tBodyGyroStdX tBodyGyroStdY tBodyGyroStdZ
## 1     0.08494482    -0.4735355  -0.054607769    -0.3442666
## 2     0.05835955    -0.5448711   0.004105184    -0.5071687
##   tBodyGyroJerkMeanX tBodyGyroJerkMeanY tBodyGyroJerkMeanZ
## 1        -0.08999754        -0.03984287        -0.04613093
## 2        -0.12223277        -0.04214859        -0.04071255
##   tBodyGyroJerkStdX tBodyGyroJerkStdY tBodyGyroJerkStdZ tBodyAccMagMean
## 1        -0.2074219        -0.3044685        -0.4042555      -0.1369712
## 2        -0.6147865        -0.6016967        -0.6063320      -0.1299276
##   tBodyAccMagStd tGravityAccMagMean tGravityAccMagStd tBodyAccJerkMagMean
## 1     -0.2196886         -0.1369712        -0.2196886          -0.1414288
## 2     -0.3249709         -0.1299276        -0.3249709          -0.4665034
##   tBodyAccJerkMagStd tBodyGyroMagMean tBodyGyroMagStd tBodyGyroJerkMagMean
## 1        -0.07447175       -0.1609796      -0.1869784           -0.2987037
## 2        -0.47899162       -0.1267356      -0.1486193           -0.5948829
##   tBodyGyroJerkMagStd fBodyAccMeanX fBodyAccMeanY fBodyAccMeanZ
## 1          -0.3253249    -0.2027943    0.08971273    -0.3315601
## 2          -0.6485530    -0.4043218   -0.19097672    -0.4333497
##   fBodyAccStdX fBodyAccStdY fBodyAccStdZ fBodyAccMeanFreqX
## 1   -0.3191347   0.05604001  -0.27968675        -0.2075484
## 2   -0.3374282   0.02176951   0.08595655        -0.4187350
##   fBodyAccMeanFreqY fBodyAccMeanFreqZ fBodyAccJerkMeanX fBodyAccJerkMeanY
## 1         0.1130936        0.04972652        -0.1705470       -0.03522552
## 2        -0.1606972       -0.52011479        -0.4798752       -0.41344459
##   fBodyAccJerkMeanZ fBodyAccJerkStdX fBodyAccJerkStdY fBodyAccJerkStdZ
## 1        -0.4689992       -0.1335866        0.1067399       -0.5347134
## 2        -0.6854744       -0.4619070       -0.3817771       -0.7260402
##   fBodyAccJerkMeanFreqX fBodyAccJerkMeanFreqY fBodyAccJerkMeanFreqZ
## 1            -0.2092620            -0.3862371            -0.1855303
## 2            -0.3770231            -0.5094955            -0.5511043
##   fBodyGyroMeanX fBodyGyroMeanY fBodyGyroMeanZ fBodyGyroStdX fBodyGyroStdY
## 1     -0.3390322     -0.1030594     -0.2559409    -0.5166919   -0.03350816
## 2     -0.4926117     -0.3194746     -0.4535972    -0.5658925    0.15153891
##   fBodyGyroStdZ fBodyGyroMeanFreqX fBodyGyroMeanFreqY fBodyGyroMeanFreqZ
## 1    -0.4365622          0.0147845        -0.06577462       0.0007733216
## 2    -0.5717078         -0.1874502        -0.47357479      -0.1333739043
##   fBodyAccMagMean fBodyAccMagStd fBodyAccMagMeanFreq
## 1      -0.1286235     -0.3980326          0.19064372
## 2      -0.3523959     -0.4162601         -0.09774335
##   fBodyBodyAccJerkMagMean fBodyBodyAccJerkMagStd
## 1              -0.0571194             -0.1034924
## 2              -0.4426522             -0.5330599
##   fBodyBodyAccJerkMagMeanFreq fBodyBodyGyroMagMean fBodyBodyGyroMagStd
## 1                  0.09382218           -0.1992526          -0.3210180
## 2                  0.08535241           -0.3259615          -0.1829855
##   fBodyBodyGyroMagMeanFreq fBodyBodyGyroJerkMagMean
## 1                0.2688444               -0.3193086
## 2               -0.2193034               -0.6346651
##   fBodyBodyGyroJerkMagStd fBodyBodyGyroJerkMagMeanFreq
## 1              -0.3816019                    0.1906634
## 2              -0.6939305                    0.1142773

Types of Activities and Summary

activities=c("subject", "activity", dtTidy)
str(activities[4])
## List of 1
##  $ activity: Factor w/ 6 levels "LAYING","SITTING",..: 4 6 5 2 3 1 4 6 5 2 ...
summary(activities[4])
##          Length Class  Mode   
## activity 180    factor numeric