-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
112 lines (99 loc) · 3.75 KB
/
Copy pathrun_analysis.R
File metadata and controls
112 lines (99 loc) · 3.75 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
107
108
109
110
111
# run_analysis.R
library(dplyr)
# http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones
# Here are the data for the project:
# https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip
# You should create one R script called run_analysis.R that does the following.
# Obtain the data, unless we already have it
# Grab our current WD, and save it; then go to the chosen work directory so
# that we'll leave the WD where it started
oldwd <- getwd()
if (!file.exists("rawdata.zip")) {
setInternet2(TRUE)
sourceurl <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
destfile <-"./rawdata.zip"
download.file(url = sourceurl, destfile = destfile, method = "internal", mode = "wb")
} else {
print("File already exists in directory, not downloading.")
}
# Unzip the rawdata.zip file, if necessary
if (!file.exists("UCI HAR Dataset")) {
print("Unzipping the raw data.")
unzip(destfile)
} else {
print ("Dataset already unzipped.")
}
# Merges the training and the test sets to create one data set.
# 1) Get the activity labels and features
print("Getting the training dataset.")
setwd("UCI HAR Dataset")
activities <- read.table("activity_labels.txt")
activities <- as.vector(activities$V2)
features <- read.table("features.txt")
features <- as.vector(features$V2)
# 2) Read the test dataset into data
print("Get the test dataset.")
setwd("test")
# testd is a data.frame holding the test data with activity numbers in first col.
data <- read.table("X_test.txt")
acts <- read.table("y_test.txt")
subjects <- read.table("subject_test.txt")
testd <- cbind(subjects,acts,data)
# 3) Read the training dataset into traind; remove data and acts when done
setwd("../train")
data <- read.table("X_train.txt")
acts <- read.table ("y_train.txt")
subjects <- read.table("subject_train.txt")
traind <- cbind(subjects,acts,data)
# 4) Combine the two datasets
data <- rbind(testd, traind)
rm(testd, traind)
# Add column names
vars <- c("Subject_ID","Activity", as.vector(features))
colnames(data) <- vars
# Extract the columns for means and standard deviations
ms <- grep("mean()",names(data))
ss <- grep("std()", names(data))
keeps <- c(1,2,ms, ss)
rm(ms,ss)
data <- data[,keeps]
# Change first column to descriptive activity names
print ("Adding descriptive names.")
for(i in 1:nrow(data)){
index <- as.integer(data[i,2])
data[i,2] <- activities[index]
}
# Creates a second, independent tidy data set with the average of each variable for each activity and each subject.
print("Creating data table frame from data frame.")
data <- tbl_df(data)
# Filter out each group, drop the first two columns (which we won't need once
# we filter) and then use colMeans to get the averages. Build this into
# a new data frame called result
print("Filtering and averaging.")
result <- data.frame(row.names = colnames(data))
for (subj in 1:30) {
for (act in activities) {
tempdf <- filter(data, Subject_ID == subj, Activity == act)
tempdf <- tempdf[-c(1,2)]
result <- rbind(result, colMeans(tempdf))
}
}
# Add descriptive naes
i <- 1
acts <- ""
subs <- ""
for (subj in 1:30) {
for (act in activities) {
# build vectors that we can cbind onto result
acts <- c(acts, act)
subs <- c(subs, subj)
}
}
result <- cbind(acts[2:length(acts)], result)
result <- cbind(subs[2:length(subs)], result)
colnames(result) <- names(data)
rownames(result) <- NULL
# Write the final dataset in our starting directory
print ("writing final dataset.")
setwd(oldwd)
write.table(result, file = "tidy_avg.txt", row.names = FALSE)