-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun_analysis.R
More file actions
61 lines (38 loc) · 1.61 KB
/
run_analysis.R
File metadata and controls
61 lines (38 loc) · 1.61 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
setwd("~/")
# Column names for data frame
columns <- read.delim("UCI HAR Dataset/features.txt", header = FALSE, sep=" ", stringsAsFactors = FALSE)
# clean labels
columns$V2 <- sub("\\(", "", columns$V2)
columns$V2 <- sub("\\)", "", columns$V2)
columns$V2 <- sub(",", "", columns$V2)
columns$V2 <- gsub("-", "_", columns$V2)
#Activity names
activity.names <- read.table("UCI HAR Dataset/activity_labels.txt")
names(activity.names) <- c("actIndex", "activity")
## Test Data
# data file
x_test <- read.table(file = "UCI HAR Dataset/test/X_test.txt", header = FALSE)
# activity label
labels_test <- read.delim("UCI HAR Dataset/test/y_test.txt", header = FALSE)
# subject number
subject_test <- read.delim("UCI HAR Dataset/test/subject_test.txt", header = FALSE)
## Train Data
# data file
x_train <- read.table(file = "UCI HAR Dataset/train/X_train.txt", header = FALSE)
# activity label
labels_train <- read.delim("UCI HAR Dataset/train/y_train.txt", header = FALSE)
# subject number
subject_train <- read.delim("UCI HAR Dataset/train/subject_train.txt", header = FALSE)
## Merge and label sets
x_combined <- rbind(x_test, x_train)
names(x_combined) <- columns$V2
# Extract only the mean() and std() fields
set <- x_combined[,grep("mean|std",names(x_combined))]
set$subject <- rbind(subject_test, subject_train)[,1]
set$activity <- rbind(labels_test, labels_train)[,1]
#label activity
set$activity <- activity.names[set$activity,2]
#Buld dataframe for results
result <- aggregate(set[,1:79], by=list(Subject=set$subject, Activity=set$activity), mean)
# write tidy result out
write.table(result, "tidy_result.txt", row.name = FALSE)