-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
46 lines (40 loc) · 1.81 KB
/
run_analysis.R
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
#extract data from text file
train=read.table("~/UCI HAR Dataset/train/X_train.txt");
test=read.table("~/UCI HAR Dataset/test/X_test.txt");
#Merges the training and the test sets to create one data set
fulldata = rbind(train,test)
#extract features from text file
fullfeatures=read.table("features.txt");
features = as.character(fullfeatures$V2);
#Extracts only the measurements on the mean and standard deviation for each measurement
no = 0;
num = numeric();
colnum = 1;
for (colnum in 1:length(vector)){
if (!is.na(grep("mean()",vector[colnum],fixed=T)||grep("std()",vector[colnum],fixed=T))){
no = no + 1;
num[no] = colnum;
}
colnum = colnum + 1;
}
#Appropriately labels the data set with descriptive variable names
tidyname = vector[num];
tidydata = fulldata[num];
names(tidydata) = tidyname;
#extract and match the activity label
#Uses descriptive activity names to name the activities in the data set
rawtrainlabel = read.table("~/UCI HAR Dataset/train/y_train.txt");
rawtestlabel = read.table("~/UCI HAR Dataset/test/y_test.txt");
fulllabel = rbind(rawtrainlabel,rawtestlabel);
activity = read.table("activity_labels.txt");
activitylabel = factor(as.numeric(fulllabel$V1),labels=as.character(activity$V2));
#extract and match the identifier of subject
trainsubject = read.table("~/UCI HAR Dataset/train/subject_train.txt");
testsubject = read.table("~/UCI HAR Dataset/test/subject_test.txt");
subject = cbind(t(as.numeric(trainsubject$V1)),t(as.numeric(testsubject$V1)));
#Creates a second, independent tidy data set with the average of each variable
#for each activity and each subject
newdata = cbind(t(subject),activitylabel,tidydata);
complete=aggregate(tidydata,by=list(t(subject),activitylabel),FUN="mean")
names(complete)[c(1,2)]=c("Subject","Activity");
write.table(complete,"TidyData.txt");