GithubHelp home page GithubHelp logo

getdatacourseproject2's Introduction

Getting and Cleaning Data - Course Project 2

ennpet
27. september 2015

Goal 1 - Merging the training and the test sets to create one data set.

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)

Goal 3 - Using descriptive activity names to name the activities in the data set

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 )

Goal 4- Appropriately label the data set with descriptive variable names.

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"

Goal 1 - finishing merging the datasets

Now I have mearged the subject, activity and feature datasets by columns.

x <- cbind(subject, y, x)

Goal 2 - extracting only the measurements on the mean and standard deviation for each measurement.

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

Contributors

ennpet avatar

Watchers

 avatar

Forkers

shaolb2000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.