-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
54 lines (39 loc) · 2.01 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
library(data.table)
library(plyr)
library(dplyr)
select_mean_std_columns <- function(df) {
as.data.frame(df)[, grepl("subject", names(df)) | grepl("activity", names(df)) | grepl("mean", names(df)) | grepl("std", names(df))]
}
run_analysis <- function () {
# read features
features <- read.table("data/features.txt")
feature_names <- as.character(features$V2)
# read activity vector for test data
test_activity <- read.table("data/test/y_test.txt")
# read test subjects
test_subjects <- read.table("data/test/subject_test.txt")
# bind activity as first column to test data
df_test <- cbind(test_subjects, test_activity, read.table("data/test/X_test.txt"))
setnames(df_test, c("subject", "activity", feature_names))
# select mean and std columns
df_test <- select_mean_std_columns(df_test)
# read activity vector for training data
train_activity <- read.table("data/train/y_train.txt")
# read train subjects
train_subjects <- read.table("data/train/subject_train.txt")
# bind activity as first column to training data
df_train <- cbind(train_subjects, train_activity, read.table("data/train/X_train.txt"))
setnames(df_train, c("subject", "activity", feature_names))
# select mean and std columns
df_train <- select_mean_std_columns(df_train)
# combine test and training data
df <- rbindlist(list(df_test, df_train))
# read activity labels
activity_labels <- read.table("data/activity_labels.txt")
# replace activity indexes with activity labels
df$activity <- factor(df$activity, levels = activity_labels$V1, labels = activity_labels$V2)
# group by subject and activity
df <- ddply(df, .(subject,activity), numcolwise(ave))
# get rid of duplicate rows
distinct_(df)
}