-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
48 lines (36 loc) · 1.56 KB
/
run_analysis.R
File metadata and controls
48 lines (36 loc) · 1.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
#library("plyr")
library("reshape2")
X_TEST <<- NULL
X_TRAIN <<- NULL
run_analysis <- function(dir="UCI HAR Dataset") {
activity_labels <- read.table(file.path(dir, "activity_labels.txt"))[,2]
features <- read.table(file.path(dir, "features.txt"), col.names=c('n','name'))[,2]
keep_features = features[grep("-(mad|max|min|sma|mad|energy|iqr|entropy|maxInds|meanFreq|skewness|kurtosis)..$", features, invert=TRUE)]
case <- "test"
if (is.null(X_TEST)) {
X_TEST <<- read.table(file.path(dir, case, "X_test.txt"), col.names=features)
}
X_TEST <- X_TEST[, keep_features]
more <- data.frame(
subject=read.table(file.path(dir, case, "subject_test.txt"))[,1],
activity=sapply(read.table(file.path(dir, case, "y_test.txt"))[,1], function(x) activity_labels[x])
)
X_TEST <- cbind(more, X_TEST)
case <- "train"
if (is.null(X_TRAIN)) {
X_TRAIN <<- read.table(file.path(dir, case, "X_train.txt"), col.names=features)
}
X_TRAIN <- X_TRAIN[, keep_features]
more <- data.frame(
subject=read.table(file.path(dir, case, "subject_train.txt"))[,1],
activity=sapply(read.table(file.path(dir, case, "y_train.txt"))[,1], function(x) activity_labels[x])
)
X_TRAIN <- cbind(more, X_TRAIN)
data <- rbind(X_TEST, X_TRAIN)
data
}
har.data <- run_analysis()
#har.data.wide <- dcast(har.data, activity + subject ~ variable, mean)
subject_activity.long = melt(har.data, id = c("subject", "activity"))
subject_activity <- dcast(subject_activity.long, activity + subject ~ variable, mean)
write.table(subject_activity, "har-data.txt", row.names=FALSE)