-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeBook.Rmd
More file actions
111 lines (79 loc) · 2.62 KB
/
CodeBook.Rmd
File metadata and controls
111 lines (79 loc) · 2.62 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
---
title: "Getting and Cleaning Data Course Project"
output: html_document
---
#Load the dplyr package
```{r}
library(dplyr)
```
#Define file name to save and target url
```{r}
zipfilename<-"Dataset.zip"
zipfileurl<-"https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
```
#check if already downloaded and un-ziped before, otherwise download and unzip the data
```{r}
if (!file.exists(zipfilename)){
download.file(url=zipfileurl, destfile=zipfilename)
unzip(zipfilename)
}
```
#Step 1: Merges the training and the test sets to create one data set.
```{r}
#read headers files
features <- read.table("UCI HAR Dataset/features.txt")
activities <- read.table("UCI HAR Dataset/activity_labels.txt")
#read all files needed
x_train <- read.table("UCI HAR Dataset/train/X_train.txt")
y_train <- read.table("UCI HAR Dataset/train/y_train.txt")
subject_train <- read.table("UCI HAR Dataset/train/subject_train.txt")
x_test <- read.table("UCI HAR Dataset/test/X_test.txt")
y_test <- read.table("UCI HAR Dataset/test/y_test.txt")
subject_test <- read.table("UCI HAR Dataset/test/subject_test.txt")
#merge the data train test
x_data <- rbind(x_train, x_test)
y_data <- rbind(y_train, y_test)
#merge subject
subject_data <- rbind(subject_train, subject_test)
#name the variables
names(x_data)<- features$V2
names (y_data)[1]<- "activity"
names(subject_data)[1]<- "subject"
#combine all data
all_data<-cbind(subject_data, y_data, x_data)
```
#Step 2. Extracts only the measurements on the mean and standard deviation for each measurement.
```{r}
tidy_data<- all_data %>% select(subject, activity, contains("mean"), contains("std"))
```
#Step 3. Uses descriptive activity names to name the activities in the data set
```{r}
tidy_data$activity<- activities[tidy_data$activity,2]
```
#Step 4. Appropriately labels the data set with descriptive variable names.
```{r}
names(tidy_data)<-gsub("^t", "time", names(tidy_data))
names(tidy_data)<-gsub("^f", "Frequency", names(tidy_data))
names(tidy_data)<-gsub("Acc", "Accelerometer", names(tidy_data))
names(tidy_data)<-gsub("Gyro", "Gyroscope", names(tidy_data))
names(tidy_data)<-gsub("BodyBody", "Body", names(tidy_data))
```
```{r}
names(tidy_data)<-gsub("Mag", "Magnitude", names(tidy_data))
```
```{r}
str(tidy_data)
```
#Step 5. From the data set in step 4, creates a second, independent tidy data set with the average of each variable for each activity and each subject.
```{r}
f_data <-tidy_data %>%
group_by(subject, activity) %>%
summarise_all(mean)
```
```{r}
str(f_data)
```
```{r}
#write the final data
write.table(f_data, "finaldata.txt", row.name=FALSE)
```