The run_analysis.r script creates a tidy data set containing the means of each variable in each activity for each subject from the human movement smartphone data from the UCI Machine Learning Repository
This document describes the steps that run_analysis.r file executes in order to prepare the final data set
#The repository includes the following files:#
- 'README.md' - this file contains the step by step instructions and R syntax to recreate the data set assuming that the 'UCI HAR Dataset' folder is in the same directory as the 'run_analysis.r' file
- 'CodeBook.md' - a file that lists the variables and describes the data
- 'run_analysis.r' - an r script file that produces the tidy data set
library(plyr) ###
First load the feature list featureList<-read.table("./UCI HAR Dataset/features.txt",sep=" ")
Next load the test data subject_test<-read.table("./UCI HAR Dataset/test/subject_test.txt",sep=" ") y_test<-read.table("./UCI HAR Dataset/test/y_test.txt",sep=" ") X_test<-read.table("./UCI HAR Dataset/test/X_test.txt")
Next load the training data subject_train<-read.table("./UCI HAR Dataset/train/subject_train.txt",sep=" ") y_train<-read.table("./UCI HAR Dataset/train/y_train.txt",sep=" ") X_train<-read.table("./UCI HAR Dataset/train/X_train.txt")
Original data from here Original data taken from the UCI Machine Learning Repository here test_data<-cbind(subject_test,y_test, X_test) #combine test data train_data<-cbind(subject_train,y_train, X_train) #combine training data
All_data<-rbind(train_data,test_data) #combine test and training data
namesList<-featureList[,2] #get the raw variable names
newNamesList<-vector("character",length(namesList)) #initialize a vector for new
#variable names where the
#punctuation characters are
#removed
#remove punctation from variable names
for (i in 1:length(namesList)){
newNamesList[i]<-gsub("[[:punct:]]", "", namesList[i])
}
newNamesList<-c("SubjectID","ActivityIndex",newNamesList)
names(All_data)<-newNamesList #apply names
VarSel<-c(1:2,c(1:6,41:46,81:86,121:126,161:166,201:202,214:215,227:228,240:241,
253:254,266:271,345:350,424:429,503:504,
516:517,529:530,542:543)+2) #identify the columns
All_data_MeansStd<-All_data[,VarSel] #extract the relevant columns
#sort the data according to respondent ID and Activity
All_data_MeansStd<-All_data_MeansStd[order(All_data_MeansStd$SubjectID, All_data_MeansStd$ActivityIndex),]
All_data_MeansStd$ActivityIndex <- factor(All_data_MeansStd$ActivityIndex,
levels = c(1,2,3,4,5,6),
labels = c("WALKING", "WALKING_UPSTAIRS",
"WALKING_DOWNSTAIRS", "SITTING",
"STANDING", "LAYING"))
Create a new data set that contains the average for each variable, for each activity and each subject
SubjectMeans<-ddply(All_data_MeansStd, .(SubjectID, ActivityIndex), numcolwise(mean))
write.table(SubjectMeans,"./SubjectMeans.txt",sep=",")