-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
85 lines (62 loc) · 3.13 KB
/
Copy pathrun_analysis.R
File metadata and controls
85 lines (62 loc) · 3.13 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
##Load packages for script
library(dplyr)
library(tidyr)
## set function
run_analysis <- function() {
## store the working directory for later
wd <- getwd()
## read the "features.txt" to create column names for "x" and select "mean" and "std" computations
features <- read.table("./UCI HAR Dataset/features.txt")
## change working directory to read "test" data
setwd("./UCI HAR Dataset/test")
## read 3 "test" data files to temporary variables in order to combine them
s_test <- read.table("subject_test.txt", header = FALSE, col.names = "Subject", stringsAsFactors=FALSE)
y_test <- read.table("y_test.txt", header = FALSE, col.names = "Activity", stringsAsFactors=FALSE)
x_temp <- read.table("x_test.txt", header = FALSE, col.names = features[,2], stringsAsFactors=FALSE)
## Use GREP to select only the "mean" and "std" values from the "x_test.txt"
x_test <- x_temp[,grep("mean\\(\\)|std\\(\\)", features[,2])]
## remove temp variable
rm(x_temp)
## Combine "test" datasets
test <- cbind(s_test, y_test, x_test)
## Unload "test" temp variables
rm(s_test)
rm(x_test)
rm(y_test)
##change working directory to read "train" data
setwd(paste(wd, "/UCI HAR Dataset/train", sep = ""))
## read 3 "train" data files to temporary variables in order to combine them
s_train <- read.table("subject_train.txt", header = FALSE, col.names = "Subject", stringsAsFactors=FALSE)
y_train <- read.table("y_train.txt", header = FALSE, col.names = "Activity", stringsAsFactors=FALSE)
x_temp <- read.table("x_train.txt", header = FALSE, col.names = features[,2], stringsAsFactors=FALSE)
## Use GREP to select only the "mean" and "std" values from the "x_test.txt"
x_train <- x_temp[,grep("mean\\(\\)|std\\(\\)", features[,2])]
## remove temp variable
rm(x_temp)
## Combine "test" datasets
train <- cbind(s_train, y_train, x_train)
## Unload "train" temp variables
rm(s_train)
rm(x_train)
rm(y_train)
## reset the working directory
setwd(wd)
## Combine "train" and "test" dataframes
data <- rbind(train, test)
## Clean column names by removing periods that were substituted by read.table for special characters
names(data) <- gsub("\\.", "", names(data))
## Read in "activity_labels.txt" in order to replace numerical factor with descriptive "Activity" values
activity <- read.table(paste(wd, "/UCI HAR Dataset/activity_labels.txt", sep = ""), stringsAsFactors = FALSE)
## For loop to replace values
for(i in 1:6){
data$Activity[data$Activity == i] <- activity[i, 2]
}
write.table(data, file = "CombinedData_Analysis.txt", row.name=FALSE)
## Use dplyr to group data by "Subject" and "Activity" and calculate means for each.
grouped <- data %>% group_by(Subject, Activity) %>%
summarise_each(funs(mean)) %>%
## Use tidyr to gather measurement columns and combine into two variables
gather(measure, mean, -Subject, -Activity)
## Output tidy data table of means of the measurements for each subject and activity
write.table(grouped, file = "TidyRun_Analysis.txt", row.name=FALSE)
}