-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
94 lines (79 loc) · 2.66 KB
/
run_analysis.R
File metadata and controls
94 lines (79 loc) · 2.66 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
86
87
88
89
90
91
92
93
94
#
# LIBRARIES
#
library(data.table)
library(dplyr)
#
# FUNCTIONS
#
#
# read_set
#
# Returns the specified data set ("test" or "train") from the specified directory
# as a single data table containing
# - measurements
# - activity labels; and
# - subject identifiers
#
read_set <- function(directory, set) {
# Read the list of measurement labels corresponding to columns in the data set
measurement_labels <- read.table(sprintf("%s/features.txt", directory))[,2]
# Set up a lookup table of activity labels (activity_id -> activity)
activity_labels <- read.table(sprintf("%s/activity_labels.txt", directory)) %>%
tbl_dt %>%
setNames(c("activity_id", "activity")) %>%
setkey(activity_id)
# Read activity and subject identifiers for this data set
activity_ids <- read.table(sprintf("%s/%s/y_%s.txt", directory, set, set), colClasses = "numeric")
subject_ids <- read.table(sprintf("%s/%s/subject_%s.txt", directory, set, set), colClasses = "numeric")
measurements <- read.table(sprintf("%s/%s/X_%s.txt", directory, set, set), colClasses = "numeric") %>%
setNames(measurement_labels) %>%
tbl_dt %>%
mutate(
activity = activity_labels[activity_ids]$activity,
subject_id = subject_ids
)
}
#
# merge_sets
#
# Returns the specified list of data sets, combined by rows into a single tbl_dt.
merge_sets <- function(set_list) {
tbl_dt(rbindlist(set_list))
}
#
# select_columns
#
# Selects the columns containing the mean and standard deviation for each measurement,
# while retaining the two key columns, activity and subject_id.
select_columns <- function (set) {
select(
set,
grep("activity|subject_id|((mean|std)\\(\\))", names(set))
)
}
#
# get_averages
#
# Returns the average for each variable in the specified set, grouped by the key columns, activity and subject_id
get_averages <- function (set) {
set %>%
group_by(activity, subject_id) %>%
summarise_each(funs(mean))
}
#
# EXECUTION
#
# From the specified data directory..
datadir <- "UCI HAR Dataset"
# 1) Get the two data sets with descriptive column names and activity labels applied
test_set <- read_set(datadir, "test")
train_set <- read_set(datadir, "train")
# 2) Merge the training and the test sets to create one data set
merged_set <- merge_sets(list(train_set, test_set))
# 3) Extract only the measurements on the mean and standard deviation for each measurement
selected_set <- select_columns(merged_set)
# 4) Create another data set with the average of each variable for each activity and each subject.
average_set <- get_averages(selected_set)
# 5) Write the average data set into a txt file
write.table(average_set, file = "averages.txt", row.names = FALSE)