-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
64 lines (50 loc) · 2.06 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
# You should set wd to current file
# setwd(...)
# You should get library reshape2
# Download data for analysis
temp <- tempfile()
download.file("https://d396qusza40orc.cloudfront.net/getdata/projectfiles/UCI HAR Dataset.zip",
destfile = temp, method = "wget",
quiet = FALSE, mode = "w",cacheOK = TRUE,
extra = getOption("download.file.extra"))
# unzip file
unzip(temp,exdir = getwd())
# remove temp file
unlink(temp)
# Read X training and test data
xTrain <- read.table("UCI HAR Dataset/train/X_train.txt")
xTest <- read.table("UCI HAR Dataset/test/X_test.txt")
#
xData <- rbind(xTest, xTrain)
# Read Y training and test data
yTrain <- read.table("UCI HAR Dataset/train/y_train.txt")
yTest <- read.table("UCI HAR Dataset/test/y_test.txt")
yData <- rbind(yTest, yTrain)
# Read subject train and test data
sTrain <- read.table("UCI HAR Dataset/train/subject_train.txt")
sTest <- read.table("UCI HAR Dataset/test/subject_test.txt")
sData <- rbind(sTrain, sTest)
# Read activities data and label Y_data file
activities <- read.table("UCI HAR Dataset/activity_labels.txt")
activities[, 2] = gsub("_", "", tolower(as.character(activities[, 2])))
yData [,1] = activities[yData[,1], 2]
names(yData) <- "activity"
# Read features.txt file.
# Get only mean and standard deviation for each measurement.
# Label the mean and std data set (xDataMS) dataset
features <- read.table("UCI HAR Dataset/features.txt")
mSFeatures <- grep("-mean\\(\\)|-std\\(\\)", features[, 2])
xDataMS <- xData[,mSFeatures]
names(xDataMS)<-tolower(gsub("\\(|\\)", "", features[mSFeatures, 2]))
# Put labels for sData
# merge datasets
names(sData) <- "subject"
tidyDatasetA <- cbind(sData, yData, xDataMS)
write.table(tidyDatasetA, "merged_tidy_data.txt")
# Creates a second, independent tidy data set
# with the average of each variable
# for each activity and each subject.
require(reshape2)
mData <- melt(tidyDatasetA, id=c("subject","activity"))
tidyAvgData <- dcast(mData, formula = subject activity ~ variable, mean)
write.table(tidyAvgData, "tidyAvgData.txt")