-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
60 lines (38 loc) · 2.23 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
# Authour: Jakob Berg
# June 2021
library(dplyr)
# Import #
data_dir = "./getdata_projectfiles_UCI HAR Dataset/UCI HAR Dataset/"
features <- read.table(paste0(data_dir, "features.txt"), col.names = c("row", "functions"))
activity_labels <- read.table(paste0(data_dir, "activity_labels.txt"), col.names = c("class", "activity"))
test_data_dir = "./getdata_projectfiles_UCI HAR Dataset/UCI HAR Dataset/test/"
subject_test <- read.table(paste0(test_data_dir, "subject_test.txt"), col.names = "subject")
x_test <- read.table(paste0(test_data_dir, "X_test.txt"), col.names = features$functions)
y_test <- read.table(paste0(test_data_dir, "y_test.txt"), col.names = "class")
train_data_dir = "./getdata_projectfiles_UCI HAR Dataset/UCI HAR Dataset/train/"
subject_train <- read.table(paste0(train_data_dir, "subject_train.txt"), col.names = "subject")
x_train <- read.table(paste0(train_data_dir, "X_train.txt"), col.names = features$functions)
y_train <- read.table(paste0(train_data_dir, "y_train.txt"), col.names = "class")
# Tidy #
## 1) Merge the training and the test sets to create one data set.
x_df <- rbind(x_test, x_train)
y_df <- rbind(y_test, y_train)
subject_df <- rbind(subject_test, subject_train)
HAR_df <- cbind(x_df, y_df, subject_df)
## 2) Extract only the measurements on the mean and standard deviation for each
# measurement.
HAR_tidy_df <- HAR_df %>%
select(subject, class, contains("mean"), contains("std"))
## 3) Use descriptive activity names to name the activities in the data set.
HAR_tidy_df <- inner_join(HAR_tidy_df, activity_labels, by = "class") %>%
select(-class) %>% # Drop class var
relocate(subject, activity) # Reorder vars to front
## 4) Appropriately label the data set with descriptive variable names.
names(HAR_tidy_df) <- gsub("Acc", "Accelerometer", names(HAR_tidy_df))
names(HAR_tidy_df) <- gsub("Gyro", "Gyroscope", names(HAR_tidy_df))
names(HAR_tidy_df) <- gsub("BodyBody", "Body", names(HAR_tidy_df))
## 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.
HAR_agg_df <- HAR_tidy_df %>%
group_by(activity, subject) %>%
summarize_all(funs(mean))