-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
66 lines (50 loc) · 1.86 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
61
62
63
64
65
66
require(dplyr)
# Read feature (e.g measurement) descriptions
features_df <- read.table("features.txt")
# Read activity labels
act_df <- read.table("activity_labels.txt")
# Read test data sets
xtest_df <- read.table("test/X_test.txt")
subject_test_df <- read.table("test/subject_test.txt")
ytest_df <- read.table("test/y_test.txt")
ytest_df[,1] <- act_df[ytest_df[,1],2]
# Combine them into one data frame
test_df <- cbind(ytest_df, xtest_df)
test_df <- cbind(subject_test_df, test_df)
# Remove test data sets from environment
rm(xtest_df)
rm(subject_test_df)
rm(ytest_df)
# Read train data sets
xtrain_df <- read.table("train/X_train.txt")
subject_train_df <- read.table("train/subject_train.txt")
ytrain_df <- read.table("train/y_train.txt")
ytrain_df[,1] <- act_df[ytrain_df[,1],2]
# Combine them into one data frame
train_df <- cbind(ytrain_df, xtrain_df)
train_df <- cbind(subject_train_df, train_df)
# Rm train data sets from environment
rm(xtrain_df)
rm(subject_train_df)
rm(ytrain_df)
## Merge test and train data frames into one jumbo data frame
df <- rbind(train_df, test_df)
# Build column names
col_names <- c("Subject", "Activity", as.character(features_df[,2]))
# and assign these names to our data frame, removing "funky" characters
# from the names or dplyr functions will choke
names(df) <- gsub("[(),-]", "", col_names)
# We have a problem: select{dplyr} considers some column names as duplicate
# Fortunately, we don't really need these columns
# So just remove them now
udf <- df[,!duplicated(names(df))]
rm(df)
tbl <- tbl_df(udf)
rm(udf)
xtb <- tbl %>%
select(Subject, Activity, contains("std", ignore.case = FALSE),
contains("mean", ignore.case = FALSE)) %>%
group_by(Subject, Activity) %>%
summarise_each(funs(mean(., na.rm = TRUE)), matches("std|mean"))
print(xtb)
write.table(xtb, file = "result.txt", row.name = FALSE)