-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
56 lines (45 loc) · 2.07 KB
/
run_analysis.R
File metadata and controls
56 lines (45 loc) · 2.07 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
#Getting and Cleaning Data
#Course Project
#Mackenzie Wildman
#1/22/17
#input all data ###############################################################
#each row gives a 561-feature vector with time and frequency domain variables
trainX <- read.table("./UCI HAR Dataset/train/X_train.txt")
testX <- read.table("./UCI HAR Dataset/test/X_test.txt")
#corresponding labels for features
featureslabels <- read.table("./UCI HAR Dataset/features.txt")
#which activity was performed in each window
#range: 1-6
trainy <- read.table("./UCI HAR Dataset/train/y_train.txt")
testy <- read.table("./UCI HAR Dataset/test/y_test.txt")
#corresponding activity labels
activitylabels <- read.table("./UCI HAR Dataset/activity_labels.txt")
#which subject performed each window
#range: 1-30
trainsubject <- read.table("./UCI HAR Dataset/train/subject_train.txt")
testsubject <- read.table("./UCI HAR Dataset/test/subject_test.txt")
#merge data horizontally ######################################################
#set variable names using featureslabels
names(trainX) <- featureslabels[,2]
names(testX) <- featureslabels[,2]
#add activity
trainX$activitynum <- trainy[,1]
testX$activitynum <- testy[,1]
#convert activity numbers to labels
trainX$activity <- activitylabels[trainX$activitynum,2]
testX$activity <- activitylabels[testX$activitynum,2]
#add subject
trainX$subject <- trainsubject[,1]
testX$subject <- testsubject[,1]
#merge train and test data ####################################################
mergedData <- rbind(trainX, testX)
#find features (variable names) containing any mention of mean or standard deviation
summarycolumns <- grep("([Mm]ean)|([Ss]td)",names(mergedData))
#subset on these columns plus activity and subject
tidydata <- mergedData[,c(563,564,summarycolumns)]
#make summary data ############################################################
library(reshape2) #for dcast
#melt data
datamelt <- melt(tidydata, id=c("subject","activity"))
#reshape data, summarize by mean
summarydata <- dcast(datamelt, subject + activity ~ variable, mean)