forked from sefakilic/coursera-getdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
106 lines (97 loc) · 4.03 KB
/
run_analysis.R
File metadata and controls
106 lines (97 loc) · 4.03 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
95
96
97
98
99
100
101
102
103
104
105
106
# This R script gets and performs some cleaning on human activity data, built
# from recordings of subjects performing daily activities while carrying
# smartphone. The full description of the data set is available at:
# http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones
library(plyr)
download.data = function() {
"Checks for data directory and creates one if it doesn't exist"
if (!file.exists("data")) {
message("Creating data directory")
dir.create("data")
}
if (!file.exists("data/UCI HAR Dataset")) {
# download the data
fileURL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
zipfile="data/UCI_HAR_data.zip"
message("Downloading data")
download.file(fileURL, destfile=zipfile, method="curl")
unzip(zipfile, exdir="data")
}
}
merge.datasets = function() {
"Merge training and test datasets"
# Read data
message("reading X_train.txt")
training.x <- read.table("data/UCI HAR Dataset/train/X_train.txt")
message("reading y_train.txt")
training.y <- read.table("data/UCI HAR Dataset/train/y_train.txt")
message("reading subject_train.txt")
training.subject <- read.table("data/UCI HAR Dataset/train/subject_train.txt")
message("reading X_test.txt")
test.x <- read.table("data/UCI HAR Dataset/test/X_test.txt")
message("reading y_test.txt")
test.y <- read.table("data/UCI HAR Dataset/test/y_test.txt")
message("reading subject_test.txt")
test.subject <- read.table("data/UCI HAR Dataset/test/subject_test.txt")
# Merge
merged.x <- rbind(training.x, test.x)
merged.y <- rbind(training.y, test.y)
merged.subject <- rbind(training.subject, test.subject)
# merge train and test datasets and return
list(x=merged.x, y=merged.y, subject=merged.subject)
}
extract.mean.and.std = function(df) {
# Given the dataset (x values), extract only the measurements on the mean
# and standard deviation for each measurement.
# Read the feature list file
features <- read.table("data/UCI HAR Dataset/features.txt")
# Find the mean and std columns
mean.col <- sapply(features[,2], function(x) grepl("mean()", x, fixed=T))
std.col <- sapply(features[,2], function(x) grepl("std()", x, fixed=T))
# Extract them from the data
edf <- df[, (mean.col | std.col)]
colnames(edf) <- features[(mean.col | std.col), 2]
edf
}
name.activities = function(df) {
# Use descriptive activity names to name the activities in the dataset
colnames(df) <- "activity"
df$activity[df$activity == 1] = "WALKING"
df$activity[df$activity == 2] = "WALKING_UPSTAIRS"
df$activity[df$activity == 3] = "WALKING_DOWNSTAIRS"
df$activity[df$activity == 4] = "SITTING"
df$activity[df$activity == 5] = "STANDING"
df$activity[df$activity == 6] = "LAYING"
df
}
bind.data <- function(x, y, subjects) {
# Combine mean-std values (x), activities (y) and subjects into one data
# frame.
cbind(x, y, subjects)
}
create.tidy.dataset = function(df) {
# Given X values, y values and subjects, create an independent tidy dataset
# with the average of each variable for each activity and each subject.
tidy <- ddply(df, .(subject, activity), function(x) colMeans(x[,1:60]))
tidy
}
clean.data = function() {
# Download data
download.data()
# merge training and test datasets. merge.datasets function returns a list
# of three dataframes: X, y, and subject
merged <- merge.datasets()
# Extract only the measurements of the mean and standard deviation for each
# measurement
cx <- extract.mean.and.std(merged$x)
# Name activities
cy <- name.activities(merged$y)
# Use descriptive column name for subjects
colnames(merged$subject) <- c("subject")
# Combine data frames into one
combined <- bind.data(cx, cy, merged$subject)
# Create tidy dataset
tidy <- create.tidy.dataset(combined)
# Write tidy dataset as csv
write.csv(tidy, "UCI_HAR_tidy.csv", row.names=FALSE)
}