-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
64 lines (48 loc) · 2.19 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
run_analysis <- function(export=TRUE){
#load reshape2 library
library(reshape2)
print("Starting to import data...")
# load data from working directory
X_test <- read.table("./UCI HAR Dataset/test/X_test.txt", quote="\"")
print("Test data imported")
X_train <- read.table("./UCI HAR Dataset/train/X_train.txt", quote="\"")
print("Train data imported")
y_test <- read.table("./UCI HAR Dataset/test/y_test.txt", quote="\"")
y_train <- read.table("./UCI HAR Dataset/train/y_train.txt", quote="\"")
subject_test <- read.table("./UCI HAR Dataset/test/subject_test.txt", quote="\"")
subject_train <- read.table("./UCI HAR Dataset/train/subject_train.txt", quote="\"")
features <- read.table("./UCI HAR Dataset/features.txt", quote="\"")
activity_labels <- read.table("./UCI HAR Dataset/activity_labels.txt", quote="\"")
print("All data imported")
# merge data sets
data <- rbind(X_train, X_test)
activities <- rbind(y_train, y_test)
subjects <- rbind(subject_train, subject_test)
# remove unnecessary data sets
rm("X_train","X_test","y_train","y_test","subject_train","subject_test")
# convert activities from numbers to strings
activities <- lapply(activities, function(x){out <- activity_labels$V2[x]})
rm(activity_labels)
# filter columns that contain "mean()" or "std()" and name them
filt <- grepl("mean()" ,features$V2, fixed = TRUE) | grepl("std()" ,features$V2, fixed = TRUE)
data <- data[,filt]
colnames(data) <- t(features$V2)[filt]
data <- cbind(subjects,activities,data)
colnames(data)[1] <- "subjects"
colnames(data)[2] <- "activities"
rm(features,subjects)
# order data according to subjects and activities
data <- data[order(data$subjects,data$activities),]
# melt data
meltData <- melt(data, id=c("subjects","activities"))
# cast data
tidyData <- dcast(meltData, subjects + activities ~ ..., mean)
rm(data,meltData,activities,filt)
# write data to tab-separated txt file if export == TRUe
if(export){
print("Writing tidy data to ./tidyData.txt")
write.table(tidyData, file="./tidyData.txt", sep="\t", row.names=FALSE)
}
print("Tidy data produced - end of script :)")
return(tidyData)
}