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
You should create one R script called run_analysis.R that does the following.
- Merges the training and the test sets to create one data set
- Extracts only the measurements on the mean and standard deviation for each measurement
- Uses descriptive activity names to name the activities in the data set
- Appropriately labels the data set with descriptive variable names
- 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
The run_analysis.R script contains the code from downloading the data to the clean up steps, the precedure is described as follows:
- Create directory
./dataif it doesn't exist. - Download the
dataset.zipfile if it doesn't exist. - Unzip the
dataset.zipfile in the./datadirectory.
if (!file.exists("./data")) {
dir.create("./data")
}
if (!file.exists("./data/dataset.zip")) {
fileUrl <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
download.file(fileUrl, destfile = "./data/dataset.zip", method = "curl")
}
unzip(zipfile = "./data/dataset.zip", exdir = "./data")The files that will be used in this assignment are listed as follows:
activity_labels.txt: names for the six activitiesfeatures.txt: list of all features (variables)subject_test.txt: the subject number for each row of test groupX_test.txt: the data collected of test groupy_test.txt: the activity number for each row of test groupsubject_train.txt: the subject number for each row of train groupX_train.txt: the data collected of train groupy_train.txt: the activity number for each row of train group
The final data frame will have the following columns and rows:
- Columns (variables):
- Subject: number from 1-30 (30 volunteers)
- Activity: number from 1-6 (6 activities)
- All the features from
features.txtfile
- Rows (observations):
- Values for each feature:
X_test.txtandX_train.txt - Numbers of subjects:
subject_test.txtandsubject_train.txt - Numbers of activities:
y_test.txtandy_train.txt
- Values for each feature:
The codes in run_analysis.R contain the following steps:
- Read the data of activities from
y_test.txtandy_train.txt - Read the data of subjects from
subject_test.txtandsubject_train.txt - Read the data of features from
X_test.txtandX_train.txt - Read the names of features (variables) from
features.txt
datafolder <- "./data/UCI HAR Dataset/"
# Read the data of activities
activity_test <- read.table(file.path(datafolder, "test", "y_test.txt"))
activity_train <- read.table(file.path(datafolder, "train", "y_train.txt"))
# Read the data of subjects
subject_test <- read.table(file.path(datafolder, "test", "subject_test.txt"))
subject_train <- read.table(file.path(datafolder, "train", "subject_train.txt"))
# Read the data of features
feature_test <- read.table(file.path(datafolder, "test", "X_test.txt"))
feature_train <- read.table(file.path(datafolder, "train", "X_train.txt"))
# Read the names of features (variables)
feature_name <- read.table(file.path(datafolder, "features.txt"))- Merge rows: merge the train and test data of
activity_data,subject_dataandfeature_data, respectively. The results are three data frames:activity_data: 10299 rows and 1 columnsubject_data: 10299 rows and 1 columnfeature_data: 10299 rows and 561 columns
- Merge columns: merge the three data frames obtained from the last step: the result data frame has 10299 rows and 563 columns
- Set names to variables: subject as
subject, activity asactivity, and the feature names fromfeatures.txt
# Merge rows
activity_data <- rbind(activity_train, activity_test)
subject_data <- rbind(subject_train, subject_test)
feature_data <- rbind(feature_train, feature_test)
# Merge columns
dataset <- cbind(subject_data, activity_data, feature_data)
# Set names to variables
colnames(dataset) <- c("subject", "activity", feature_name$V2)Grep columns which have mean(), std(), subject, or activity in their names
subdataset <- dataset[, grep("mean\\(\\)|std\\(\\)|subject|activity", names(dataset))]Based on the activity names in activity_labels.txt, the values in column activity are replaced as follows:
1asWALKING2asWALKING_UPSTAIRS3asWALKING_DOWNSTAIRS4asSITTING5asSTANDING6asLAYING
subdataset$activity[subdataset$activity == 1] <- "WALKING"
subdataset$activity[subdataset$activity == 2] <- "WALKING_UPSTAIRS"
subdataset$activity[subdataset$activity == 3] <- "WALKING_DOWNSTAIRS"
subdataset$activity[subdataset$activity == 4] <- "SITTING"
subdataset$activity[subdataset$activity == 5] <- "STANDING"
subdataset$activity[subdataset$activity == 6] <- "LAYING"The names of features will be fixed by the following standards:
- prefix
twill be replaced bytime Accwill be replaced byAccelerometerGyrowill be replaced byGyroscope- prefix
fwill be replaced byfrequency Magwill be replaced byMagnitudeBodyBodywill be replaced byBody()will be deleted-will be replaced by_
names(subdataset) <- gsub("^t", "time", names(subdataset))
names(subdataset) <- gsub("Acc", "Accelerometer", names(subdataset))
names(subdataset) <- gsub("Gyro", "Gyroscope", names(subdataset))
names(subdataset) <- gsub("^f", "frequency", names(subdataset))
names(subdataset) <- gsub("Mag", "Magnitude", names(subdataset))
names(subdataset) <- gsub("BodyBody", "Body", names(subdataset))
names(subdataset) <- gsub("\\(\\)", "", names(subdataset))
names(subdataset) <- gsub("-", "_", names(subdataset))7. Creates a second, independent tidy data set with the average of each variable for each activity and each subject
- Group the data frame by subject and activity
- Calculate the average of each variable by
summarise_all()
library(dplyr)
group_subject_activity <- group_by(subdataset, subject, activity)
dataset_ave <- summarise_all(group_subject_activity, mean, na.rm = TRUE)Export using write.table() with row.names = FALSE, the file was named as tidydata_ave.txt.
write.table(dataset_ave, "./tidydata_ave.txt", row.names = FALSE)