-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
60 lines (43 loc) · 2.2 KB
/
run_analysis.R
File metadata and controls
60 lines (43 loc) · 2.2 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
library(plyr)
library(dplyr)
#Download and unzip file
datalink <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
if(file.exists("datazip.zip") == F) {download.file(datalink, "datazip.zip", mode = "wb")}
unzip("datazip.zip")
setwd("UCI HAR Dataset")
#Quickly read in test and train sets.
initial<-read.table("train\\X_train.txt", nrows=5)
classes<-sapply(initial,class)
trainset<-read.table("train\\X_train.txt", colClasses=classes)
initial<-read.table("test\\X_test.txt", nrows=5)
classes<-sapply(initial,class)
testset<-read.table("test\\X_test.txt", colClasses=classes)
#Merges the training and the test sets to create one data set.
mergedset <- bind_rows(testset, trainset)
#reading in column names and labeling merged data frame
features<-read.table("features.txt")
#reading in activity labels
activity_labels<-read.table("activity_labels.txt")
#reading in test and train labels
test_labels<-read.table("test\\y_test.txt")
train_labels<-read.table("train\\y_train.txt")
#Selecting rows with only mean and std
mean_and_std_indices <- grep("mean|std",features[,2])
#Merges the training and the test labels.
mergedlabels <- bind_rows(test_labels, train_labels)
#subsetting columns
mean_and_std_set <- select(mergedset, mean_and_std_indices)
#adding varible names
colnames(mean_and_std_set) <- features[mean_and_std_indices,2]
#append column with activity labels
mean_and_std_set <- bind_cols(mean_and_std_set, mergedlabels)
#rename activity column
names(mean_and_std_set)[names(mean_and_std_set) == "V1"] <- "Activity"
#replacing activity code to description found in activity_labels file
mean_and_std_set$Activity <- mapvalues(mean_and_std_set$Activity,as.character(activity_labels[,1]),as.character(activity_labels[,2]))
#freeing memory
rm(initial, classes, testset, trainset, test_labels, train_labels, mean_and_std_indices, mergedlabels, mergedset, features, activity_labels)
#Constructing final summarized table. Grouping by activity and calculating mean of each column
final_table <- mean_and_std_set %>% group_by(Activity) %>% summarise_each(funs(mean))
#saving tidy final table with calculated means grouped by activity
write.table(final_table, file="..\\summarized.txt", row.name=F)