-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_analysis.R
More file actions
65 lines (52 loc) · 2.27 KB
/
run_analysis.R
File metadata and controls
65 lines (52 loc) · 2.27 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
58
59
60
61
62
63
64
65
#You should create one R script called run_analysis.R that does the following:
# 1) Merges the training and the test sets to create one data set.
# 2) Extracts only the measurements on the mean and standard deviation
# for each measurement.
# 3) Uses descriptive activity names to name the activities in the data set
# 4) Appropriately labels the data set with descriptive activity names.
# 5) Creates a second, independent tidy data set with the average of
# each variable for each activity and each subject.
#path relative to the working directory
#where the test data should be extracted.
#read test data
subject_test <- read.table("test/subject_test.txt", quote="\"")
X_test <- read.table("test/X_test.txt", quote="\"")
y_test <- read.table("test/y_test.txt", quote="\"")
#read training data
subject_train <- read.table("train/subject_train.txt", quote="\"")
X_train <- read.table("train/X_train.txt", quote="\"")
y_train <- read.table("train/y_train.txt", quote="\"")
#read supporting information
features <- read.table("features.txt", quote="\"")
labels <- read.table("activity_labels.txt", quote="\"")
#build tidy subject
data_subject <- rbind(subject_train,subject_test)
colnames(data_subject)[1] <- "Subject"
#build tidy X,y
data_X <- rbind(X_train, X_test)
colnames(data_X) <- features[,2]
data_y <- rbind(y_train, y_test)
#integrate label Names - This should do the trick for Question 3 & 4
data_y <- merge(x=data_y, y=labels, by=1)
colnames(data_y)[1] <- "Result"
colnames(data_y)[2] <- "ResultName"
subject <- rbind(subject_train, subject_test)
colnames(subject)[1] <- "Subject"
#Build one big Dataset
data <- cbind(subject,data_y,data_X)
#This should finish Question 1
#Keep only the first 3 columns and everything labled mean() or std()
colsToKeep <- c(1,2,3,grep("mean\\(\\)|std\\(\\)",colnames(data),ignore.case=TRUE))
data <- data[,colsToKeep]
#This should finsih Question 2
#build tidy result set as aggegate
#exclude first three columns, we use those for grouping
tidy <- aggregate.data.frame(
x=data[,c(-1,-2,-3)],
by=list(
Subject=data$Subject,
Result=data$Result,
ResultName=data$ResultName),
mean)
#Write result to a file
write.table(x=tidy,file="TidyResult.txt", sep=",")