-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
57 lines (39 loc) · 2.56 KB
/
run_analysis.R
File metadata and controls
57 lines (39 loc) · 2.56 KB
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
#This program represents R script called run_analysis.R that does the following:
# Step 1: Merges the training and the test sets to create one data set.
# Step 2: Extracts only the measurements on the mean and standard deviation for each measurement.
# Step 3: Appropriately labels the data set with descriptive variable names.
# Step 4: Uses descriptive activity names to name the activities in the data set
# Step 5: 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.
#pre-processing step
#Set working directory and build data frames from given csv files
library(dplyr)
feature_list<-read.csv("UCI HAR Dataset/features.txt", sep="", header= FALSE)
activity_labels<-read.csv("UCI HAR Dataset/activity_labels.txt", sep="", header= FALSE)
training_ds<-read.csv("UCI HAR Dataset/train/X_train.txt", sep="", header= FALSE)
training_act_ds<-read.csv("UCI HAR Dataset/train/Y_train.txt", sep="", header= FALSE)
training_subject_ds<-read.csv("UCI HAR Dataset/train/subject_train.txt", sep="", header= FALSE)
initial_col=ncol(training_ds)
training_ds<-cbind(training_ds,c(training_act_ds,training_subject_ds ))
testing_ds<-read.csv("UCI HAR Dataset/test/X_test.txt", sep="", header= FALSE)
testing_act_ds<-read.csv("UCI HAR Dataset/test/Y_test.txt", sep="", header= FALSE)
testing_subject_ds<-read.csv("UCI HAR Dataset/test/subject_test.txt", sep="", header= FALSE)
testing_ds<-cbind(testing_ds,c(testing_act_ds,testing_subject_ds ))
# Step 1: Merge the training and the test sets to create one data set.
superset<-rbind(training_ds,testing_ds)
# Step 2: Extracts only the measurements on the mean and standard deviation for each measurement.
column_df<-feature_list[grep("*mean*|*Mean*|*std*|*Std*|*dev*",feature_list[,2]),]
filter_list<-c(column_df[,1], initial_col+1, initial_col+2)
filter_list_label<-feature_list[column_df[,1],]
superset<-superset[,filter_list]
# Step 3: Appropriately labels the data set with descriptive variable names.
colnames(superset)<-c(filter_list_label$V2,"activity","subject")
# Step 4: Uses descriptive activity names to name the activities in the data set
for (i in 1:nrow(activity_labels)){
superset$activity<-gsub(i,activity_labels[i,2],superset$activity)
}
superset$activity<-as.factor(superset$activity)
superset$subject<-as.factor(superset$subject)
clean_data<-aggregate(superset,by=list(activity=superset$activity, subject =superset$subject),mean)
clean_data[,ncol(clean_data)]=NULL
clean_data[,ncol(clean_data)-1]=NULL
write.table(clean_data,"tidy.txt",sep="\t")