-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
80 lines (63 loc) · 2.64 KB
/
run_analysis.R
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
# install 'plyr' if missed
if (!require('plyr')) {
install.packages('plyr')
require('plyr')
}
# default file path separator, suitable for unix-based OS
filePathSep = '/'
# download and unpack original dataset
dirName <- 'UCI HAR Dataset'
if (!file.exists(dirName)) {
fileUrl <- 'https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip'
fileName <- 'uci_har_dataset.zip'
download.file(fileUrl, fileName, 'curl')
unzip(fileName)
}
# function for merging testing and training datasets
prepareDataSet <- function(datasetName) {
getFileName <- function(prefix) {
paste(prefix, '_', datasetName, '.txt', sep = '')
}
activities <- read.table(paste(dirName, datasetName, getFileName('y'), sep = filePathSep))
subjects <- read.table(paste(dirName, datasetName, getFileName('subject'), sep = filePathSep))
measurements <- read.table(paste(dirName, datasetName, getFileName('X'), sep = filePathSep))
cbind(activities, subjects, measurements)
}
# combine test and traing datasets to one dataset
fullDataset <- rbind(prepareDataSet('test'), prepareDataSet('train'))
# read features names
features <- read.table(paste(dirName, 'features.txt', sep = filePathSep))
# set column names
names(fullDataset) <- c('activity_id', 'Subject', features$V2)
# exclude odd columns
filteredColumns <- grep('activity_id|Subject|(mean|std)\\(', names(fullDataset))
fullDataset <- fullDataset[, filteredColumns]
# read table with activity labels
activityLabels <- read.table(paste(dirName, 'activity_labels.txt', sep = filePathSep))
names(activityLabels) <- c('id', 'Activity')
# add column with activity labels to full dataset
fullDataset <- merge(activityLabels, fullDataset, by.x = 'id', by.y = 'activity_id', all = TRUE)
# drop columt with activity id
fullDataset$id <- NULL
# rename columns to descriptive names
renameFeatures <- function(pattern, replacement) {
names(fullDataset) <<- gsub(pattern, replacement, names(fullDataset))
}
renameFeatures('mean', 'Mean')
renameFeatures('std', 'Std')
renameFeatures('^t', 'Time')
renameFeatures('^f', 'Freq')
renameFeatures('\\(\\)', '')
renameFeatures('-', '_')
# save full dataset
# write.table(fullDataset, file='full_dataset.txt')
# calculate average of each variable for each activity and each subject
avgDataset <- ddply(fullDataset, .(Activity, Subject), numcolwise(mean))
# add 'Avg' prefix to measurement columns
avgDatasetNames <- names(avgDataset)
for (i in 3:ncol(avgDataset)) {
avgDatasetNames[i] <- paste('Avg', avgDatasetNames[i], sep='')
}
names(avgDataset) <- avgDatasetNames
# save result
write.table(avgDataset, file='average_dataset.txt')