-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
64 lines (55 loc) · 2.57 KB
/
run_analysis.R
File metadata and controls
64 lines (55 loc) · 2.57 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
url <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
zipfile <- "getdata-projectfiles-UCI HAR Dataset.zip"
folder <- "UCI HAR Dataset"
## Check the existence of the dataset
## If the dataset is not exist but is downloaded in a zipfile,
## unzip the dataset
## If the dataset is not exist and the zipfile is not downloaded,
## download the dataset and unzip the dataset from the zipfile
if(!file.exists(folder)) {
if(!file.exists(zipfile)) {
download.file(url, zipfile, method = "curl")
}
unzip(zipfile)
unlink(zipfile)
}
## Read the training and the test sets
## Merge them to create one data set
X_train <- read.table("UCI HAR Dataset/train/X_train.txt")
X_test <- read.table("UCI HAR Dataset/test/X_test.txt")
X <- rbind(X_train, X_test)
## Read the complete list of of variables of each feature vector
## Label the data set with descriptive variable names
## Extract the mean and standard deviation for each measurement
features <- read.table("UCI HAR Dataset/features.txt",
colClasses = "character")[ , 2]
names(X) <- features
X <- X[ , grep("mean()|std()|Mean", features)]
## Read the training and test labels
## and ID of the subject who performed the activity for each window sample
## Merge them with the train and the test sets
y_train <- read.table("UCI HAR Dataset/train/y_train.txt")
y_test <- read.table("UCI HAR Dataset/test/y_test.txt")
y <- rbind(y_train, y_test)
names(y) <- "activities"
subject_train <- read.table("UCI HAR Dataset/train/subject_train.txt")
subject_test <- read.table("UCI HAR Dataset/test/subject_test.txt")
subject <- rbind(subject_train, subject_test)
names(subject) <- "subjects"
subject$subjects <- as.factor(subject$subjects)
dataset <- cbind(subject, y, X)
## Uses descriptive activity names to name the activities in the data set
y_labels <- read.table("UCI HAR Dataset/activity_labels.txt",
colClasses = "character")[ , 2]
dataset$activities <- y_labels[dataset$activities]
dataset$activities <- as.factor(dataset$activities)
## Create an independent tidy data set with the average of each variable
## for each activity and each subject.
library(reshape2)
library(plyr)
dataset <- melt(dataset, id.vars = c("subjects", "activities"),
measure.vars = names(dataset)[3:88])
tidydataset <- ddply(dataset, .(subjects, activities, variable), summarize,
average = mean(value))
## Output the tidy data set as a txt file.
write.table(tidydataset, "tidydataset.txt", row.names = FALSE)