-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
49 lines (35 loc) · 1.51 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
library(dplyr)
library(tidyr)
source("my_functions.R")
unzip('./getdata-projectfiles-UCI HAR Dataset.zip')
activity_df <- read.table('./UCI HAR Dataset/activity_labels.txt')
names(activity_df) <- c("activityid", "activity")
feature_df <- read.table('./UCI HAR Dataset/features.txt')
X_train <- read.table('./UCI HAR Dataset/train/X_train.txt')
y_train <- read.table('./UCI HAR Dataset/train/y_train.txt')
subject_train <- read.table('./UCI HAR Dataset/train/subject_train.txt')
X_test <- read.table('./UCI HAR Dataset/test/X_test.txt')
y_test <- read.table('./UCI HAR Dataset/test/y_test.txt')
subject_test <- read.table('./UCI HAR Dataset/test/subject_test.txt')
features <- feature_df[, 2]
names(X_train) <- features
names(X_test) <- features
rm(feature_df, features)
names(y_train) <- "activityid"
names(y_test) <- "activityid"
names(subject_train) <- "subject"
names(subject_test) <- "subject"
mydf <- rbind_list(
cbind(X_train, y_train, subject_train),
cbind(X_test, y_test, subject_test))
mydf <- merge(mydf, activity_df)
rm(X_train, y_train, subject_train, X_test, y_test, subject_test, activity_df)
tbl_df(mydf) %>%
select(matches("\\-mean\\()", ignore.case = FALSE),
matches("\\-std\\()", ignore.case = FALSE),
activity, subject) %>%
gather(variable, value, -c(subject,activity)) %>%
mutate(variable = change.featurename(variable)) %>%
group_by(subject, activity, variable) %>%
summarize(average = mean(value)) %>%
write.table(file = "tidy-data.txt", row.names = FALSE)