-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis.R
45 lines (37 loc) · 1.84 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
run <- function()
{
# load necessary libraries
library(plyr)
library(dplyr)
# load all data
subject_train <- read.csv("./UCI HAR Dataset/train/subject_train.txt", colClasses = "factor", header = FALSE)
y_train <- read.csv("./UCI HAR Dataset/train/y_train.txt", colClasses = "factor", header = FALSE)
x_train <- read.table("./UCI HAR Dataset/train/X_train.txt", colClasses = "numeric", header = FALSE)
subject_test <- read.csv("./UCI HAR Dataset/test/subject_test.txt", colClasses = "factor", header = FALSE)
y_test <- read.csv("./UCI HAR Dataset/test/y_test.txt", colClasses = "factor", header = FALSE)
x_test <- read.table("./UCI HAR Dataset/test/X_test.txt", colClasses = "numeric", header = FALSE)
# merge train and test data together
x <- rbind(x_train,x_test)
y <- rbind(y_train,y_test)
subject <- rbind(subject_train,subject_test)
# give features corresponding names
featureNames <- read.table("./UCI HAR Dataset/features.txt", header = FALSE)
names(x) <- featureNames[,2]
# only use the columnnames with mean and std
columnNames <- names(x)[grep(pattern = "(mean|std)", x = names(x))]
x <- x[,columnNames]
# map the descriptive activity labels onto the data
activity_labels <- read.table("./UCI HAR Dataset/activity_labels.txt", header = FALSE, colClasses = "character")
y[,1] <- mapvalues(y[,1], from = activity_labels[,1], to = activity_labels[,2])
# add descriptive names to the remaining columns
colnames(y) <- c("ActivityType")
colnames(subject) <- c("Subject")
# bind them all together
data <- cbind(subject, x)
data <- cbind(y,data)
# group the data on activity and subject
groupedData <- group_by(data, ActivityType, Subject)
#summarise each column with the mean function
tidyData <- summarise_each(groupedData,funs(mean))
write.table(tidyData, "tidydata.txt", row.name= FALSE)
}