-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
67 lines (56 loc) · 2.35 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
67
library(readr)
library(dplyr)
library(stringr)
# load test data
X_test <- read.table("./UCI HAR Dataset/test/X_test.txt")
# load training data
X_train <- read.table("./UCI HAR Dataset/train/X_train.txt")
# load activity label for test data
actvtyLbl_test <- read.table("./UCI HAR Dataset/test/y_test.txt")
# load activity label for train data
actvtyLbl_train <- read.table("./UCI HAR Dataset/train/y_train.txt")
# load subject data for test data
subject_test <- read.table("./UCI HAR Dataset/test/subject_test.txt")
# load subject data for train data
subject_train <- read.table("./UCI HAR Dataset/train/subject_train.txt")
# load activity_labels factors
activity_labels <- readLines("./UCI HAR Dataset/activity_labels.txt")
activity <- gsub("[0-9]", "", activity_labels)
activity <- gsub(" ", "", activity)
# load features
features <- readLines("./UCI HAR Dataset/features.txt")
# combine data
combinedData <- rbind(X_train, X_test)
# combine labels
combinedLabels <- rbind(actvtyLbl_train, actvtyLbl_test)
# combine subjects
combinedSubjects <- rbind(subject_train, subject_test)
# rename columns with features
colnames(combinedData) <- features;
# Extract mean and standard deviation
tbl_combinedData <- tbl_df(combinedData)
pattern <- "mean()|std()"
toSelect <- grep(pattern, features, perl = TRUE)
meanstdData <- select(tbl_combinedData, toSelect)
# Add activity labels
meanstdData <- mutate(meanstdData, Activity = factor(combinedLabels[,], labels = activity))
# Add subject
meanstdData <- mutate(meanstdData, Subject = as.factor(combinedSubjects[,]))
# Make Meaningful variable names
statistic <- str_extract_all(names(meanstdData), "-(.*)", simplify = TRUE)
statistic <- gsub("\\W", "", statistic)
#direction <- str_extract_all(names(meanstdData), "-[X|Y|Z]", simplify = TRUE)
#direction <- gsub("\\W", "", direction)
var <- str_extract(names(meanstdData), "[t|f](.*)-")
var <- gsub("-.*-", "", var)
var <- gsub("-", "", var)
#varname <- paste(direction,statistic,"_", var, sep = "")
varname <- paste(statistic,"_", var, sep = "")
varname <- gsub("^_", "", varname)
varname[length(varname) - 1] <- "Activity"
varname[length(varname)] <- "Subject"
colnames(meanstdData) <- varname
# Create second tidy data
secondTidyData <- group_by(meanstdData, Subject, Activity)
secondTidyData <- summarise_all(secondTidyData, mean)
write.table(secondTidyData, "tidyUCIHARDataset.txt", row.name = FALSE)