ennpet
27. september 2015
To merge the data sets i have created helper functions to read the data from files that take the type (training or test) as a parameter and read the data in temporary variables.
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
rootPath <- "UCI HAR Dataset/"
read_subject <- function (type){
read.csv(paste(rootPath, type, "/", "subject_", type, ".txt", sep = ""), header = FALSE)
}
read_X <- function (type){
read.table(paste(rootPath, type, "/", "X_", type, ".txt", sep = ""), header = FALSE,
colClasses = rep("numeric", 561))
}
read_y <- function (type){
read.csv(paste(rootPath, type, "/", "y_", type, ".txt", sep = ""), header = FALSE)
}
subject_train <- read_subject("train")
y_train <- read_y("train")
X_train <- read_X("train")
subject_test <- read_subject("test")
y_test <- read_y("test")
X_test <- read_X("test")
Then I merged training and test data sets for subjects, activities and features by rows.
subject <- rbind(subject_train, subject_test)
y <- rbind(y_train, y_test)
x <- rbind(X_train, X_test)
To assign descriptive activity names I have read the "activity_labels.txt" file to a data set and used this data set to factor activities with their corresponding labels.
activities <- read.csv(paste(rootPath, "activity_labels.txt",sep = ""),
header = FALSE, sep = " ")
y$V1 <- factor(y$V1, levels=activities$V1, labels = activities$V2 )
To label variable names i have read the "features.txt" file to a data set and used make.names() function to assign names to variables. The original feature labels have duplicate entries and the make.names() function makes the duplicate names unique by adding suffixes.
features <- read.csv(paste(rootPath, "features.txt", sep = ""),
header = FALSE, sep = " ")
names(x) <- make.names(features$V2, unique = TRUE, allow_ = TRUE)
Then I have added descriptive names to subject and activity data sets.
names(subject) <- "subject_id"
names(y) <- "activity"
Now I have mearged the subject, activity and feature datasets by columns.
x <- cbind(subject, y, x)
x.mean.std <- select(x, subject_id, activity, contains(".mean.."), contains(".std.."))
Goal 5 - From the data set in step 4, creates a second, independent tidy data set with the average of each variable for each activity and each subject.
I have grouped the resulting data set by subject and activity and summerized it using the mean() function.
x.tidy <- x.mean.std %>%
group_by(subject_id, activity) %>%
summarise_each(funs(mean))
Then I have removed extra periods in the feature variable names (made by make.name() function) and saved the data set to a file.
names(x.tidy) <-(sub(".mean..", "-mean", names(x.tidy)))
names(x.tidy) <-(sub(".std..", "-std", names(x.tidy)))
write.table(x.tidy, "tidy.txt", row.names = FALSE)
getdatacourseproject2's People
Forkers
shaolb2000Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.