-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodebook.Rmd
More file actions
124 lines (69 loc) · 4.13 KB
/
Copy pathCodebook.Rmd
File metadata and controls
124 lines (69 loc) · 4.13 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
111
112
113
114
115
116
117
118
119
120
121
---
title: "Codebook"
output:
word_document: default
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
1) The Variables
The UCI HAR Dataset is a dataset from an experiment called "Human Activity Recognition Using Smartphones Dataset".
It involved 30 participants, or subjects, (identified in the NewData as ID) and each performed six activities (WALKING, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, LAYING), wearing a smartphone, which has an embedded Accelerator and Gyroscope, used to take various measurements (features). The total number of measurements taken, for all the participants and all the activities, was 561.
2) The Data
The Dataset has 8 txt data files:
1) activity_labels.txt: labels from 1 to 6 the activities which the 30 participants
2) features.txt: labels from 1 to 561 all the 561 features measured from all participants
3) subject_test.txt: labels the subject(participants) from the test data (2, 4, 9, 10, 12, 13, 18, 20 and 24)
4) X_test.txt: complete test data set for all the 561 features measured
5) y_test.txt: indicates the activity
6) subject_train.txt: labels the subjects from the train data (1, 3, 5, 6, 7, 8, 11,
#14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30)
7) X_train.txt: complete train data set for all the 561 features measured
8) y_train.txt: indicates the activity
3) Transformations or work performed to clean up the data:
3.1) First, downloaded and extracted the data, using features as the columns names in the X databases.
```{}
features <- read.table("UCI HAR Dataset/features.txt", col.names = c("n","features"))
activities <- read.table("UCI HAR Dataset/activity_labels.txt", col.names = c("code", "activity"))
subject_test <- read.table("UCI HAR Dataset/test/subject_test.txt", col.names = "ID")
x_test <- read.table("UCI HAR Dataset/test/X_test.txt", col.names = features$features)
y_test <- read.table("UCI HAR Dataset/test/y_test.txt", col.names = "code")
subject_train <- read.table("UCI HAR Dataset/train/subject_train.txt", col.names = "ID")
x_train <- read.table("UCI HAR Dataset/train/X_train.txt", col.names = features$features)
y_train <- read.table("UCI HAR Dataset/train/y_train.txt", col.names = "code")
```
3.2) Then rbinded the following databases: X train and X test;the Y train and Y test and; Subject_test and Subject_train, in order to get various total databases.
3.4) Then cbinded the Subject_Test_Total, Y_Test_Total and X_Test_Total, in order to get the total data base, with ID participant indicator and activity indicator for both test and train datasets.
```{}
Xtotal <- rbind(x_train, x_test)
Ytotal <- rbind(y_train, y_test)
Subjecttotal <- rbind(subject_train, subject_test)
Datatotal <- cbind(Subjecttotal, Ytotal, Xtotal)
```
3.5) Using dplyr, selected just the ID and activity variables, as well as all the variables which contained mean or std in its name.
```{}
Datatotal <- Datatotal %>% select(ID, code, contains("mean"), contains("std"))
```
3.6) In order to make the dataset easier to understand, renamed the codes in Code using the Activities variable, and renamed the Code variable as Activity.
3.7) Using gsub, substituted various variable names which were in the diminutive and harder to understand.
```{}
Datatotal$code <- activities[Datatotal$code, 2]
names(Datatotal)[2] = "Activity"
names(Datatotal)<-gsub("Acc", "Accelerometer", names(Datatotal))
names(Datatotal)<-gsub("Gyro", "Gyroscope", names(Datatotal))
names(Datatotal)<-gsub("Mag", "Magnitude", names(Datatotal))
names(Datatotal)<-gsub("^t", "Time", names(Datatotal))
names(Datatotal)<-gsub("^f", "Frequency", names(Datatotal))
```
3.8) Grouped our new data set by ID and Activity and then took the average for each variable for each activity, using summarize_all.
```{}
NewDataAverage <- Datatotal %>%
group_by(ID, Activity) %>%
summarise_all(funs(mean))
```
3.9) At last, saved the new dataset as NewDataAverage and as an independent txt file.
```{}
write.table(NewDataAverage, "NewDataAverage.txt", row.name=FALSE)
```