-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
54 lines (40 loc) · 2.07 KB
/
run_analysis.R
File metadata and controls
54 lines (40 loc) · 2.07 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
run_analysis <- function(){
##Loads reshape2 package.
require(reshape2)
##Reads in the labels for the columns and the activities
anames <- read.table('UCI HAR Dataset/activity_labels.txt')
cnames <- read.table('UCI HAR Dataset/features.txt')
##Reads in the two data sets and names them using the helper functions.
test <- read_test(cnames, anames)
train <- read_train(cnames, anames)
##Combines the data into 1 large set
tdata <- rbind(test, train)
##Melts the data using subject and activity as factors.
##Recasts the data ordered by subject and activity, in that order, while also taking the mean of each subset.
##Returns the final data set.
tdata <- melt(tdata, id=c('subject', 'activity'))
tdata <- dcast(tdata, subject + activity~variable, mean)
tdata
}
read_test <- function(cnames, anames){
## Read in Features for labels
## Read in Test data set, name columns and combine
test <- read.table('UCI HAR Dataset/test/x_test.txt')
names(test) <-cnames[,2]
test <- test[, grep('-mean\\(\\)|-std\\(\\)', names(test))]
test <- cbind(read.table('UCI HAR Dataset/test/y_test.txt', col.names ='activity', colClasses = 'factor'), test)
test <- cbind(read.table('UCI HAR Dataset/test/subject_test.txt', col.names ='subject'), test)
levels(test[,'activity']) <- anames[,2]
test
}
read_train <- function(cnames, anames){
## Read in Features for labels
## Read in Test data set, name columns and combine
test <- read.table('UCI HAR Dataset/train/x_train.txt')
names(test) <-cnames[,2]
test <- test[, grep('-mean\\(\\)|-std\\(\\)', names(test))]
test <- cbind(read.table('UCI HAR Dataset/train/y_train.txt', col.names ='activity', colClasses = 'factor'), test)
test <- cbind(read.table('UCI HAR Dataset/train/subject_train.txt', col.names ='subject'), test)
levels(test[,'activity']) <- anames[,2]
test
}