-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
41 lines (23 loc) · 1.68 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
# Author: Eric Bollinger [[email protected]]
subject_train_df <- read.delim2("./UCI HAR Dataset/train/subject_train.txt", header=F, col.names = c("subject_id"))
subject_test_df <- read.delim2("./UCI HAR Dataset/test/subject_test.txt", header=F, col.names = c("subject_id"))
features_df <- read.table("./UCI HAR Dataset/features.txt", header=F, col.names = c("feature_id", "feature"))
x_train_df <- read.table("./UCI HAR Dataset/train/X_train.txt", header=F, col.names = features_df$feature)
x_test_df <- read.table("./UCI HAR Dataset/test/X_test.txt", header=F, col.names = features_df$feature)
y_train_df <- read.table("./UCI HAR Dataset/train/y_train.txt", header=F, col.names = c("activity_id"))
y_test_df <- read.table("./UCI HAR Dataset/test/y_test.txt", header=F, col.names = c("activity_id"))
activities_df <- read.table("./UCI HAR Dataset/activity_labels.txt", header=F, col.names = c("activity_id", "activity"))
y_train_df <- merge(y_train_df, activities_df, by="activity_id", all=F)
y_test_df <- merge(y_test_df, activities_df, by="activity_id", all=F)
x_train_df$activity = y_train_df$activity
x_test_df$activity = y_test_df$activity
x_train_df$subject_id = subject_train_df$subject_id
x_test_df$subject_id = subject_test_df$subject_id
library(dplyr)
x_test_df <- x_test_df %>% select(subject_id, activity, everything())
x_train_df <- x_train_df %>% select(subject_id, activity, everything())
x_combined <- rbind(x_test_df, x_train_df)
good_cols <- c(1, 2, grep("([Mm]ean|std)", colnames(x_combined)))
x_comb_ltd <- x_combined[,good_cols]
library(tidyr)
tidy_means <- x_comb_ltd %>% group_by(subject_id, activity) %>% summarize_at(.vars = names(.)[3:length(colnames(x_comb_ltd))], mean)