Giter Site home page Giter Site logo

uci_har_analysis's Introduction

UCI_HAR_Analysis

This document describes steps used by run_analysis.R to extract tidy data from Human Activity Recognition Using Smartphones Data Set.

First, libraries are loaded, variables and helper functions are defined.

# Loading libraries
print("Loading libraries")
library('data.table', quietly = T)
library('reshape', quietly = T)

# Constants
uci.datasetUrl='https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip'
uci.path=getwd()
uci.datasetPath=file.path(uci.path, 'data')

# Helpers
uci.helpers.readAsDataTable <- function (path) {
  data.table(read.table(path))
}

Download and extract dataset if we cannot locate the dataset folder.

if (!file.exists(uci.datasetPath)) {
  print("Downloading dataset");
  tmpfile <- tempfile()
  download.file(uci.datasetUrl, tmpfile, quiet = T)
  print("Extracting dataset")
  unzip(tmpfile, exdir=uci.datasetPath, junkpaths = T)
  unlink(tmpfile)
}

Load data using our helper function uci.helpers.readAsDataTable.

uciData.train.x <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'X_train.txt'))
uciData.train.y <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'y_train.txt'))
uciData.train.sub <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'subject_train.txt'))
uciData.test.x  <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'X_test.txt'))
uciData.test.y  <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'y_test.txt'))
uciData.test.sub <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'subject_test.txt'))
uciData.features <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'features.txt'))

Set descriptive name for data we just loaded.

setnames(uciData.train.y, names(uciData.train.y), c("activityId"))
setnames(uciData.test.y, names(uciData.test.y), c("activityId"))
setnames(uciData.train.sub, names(uciData.train.sub), c("subject"))
setnames(uciData.test.sub, names(uciData.test.sub), c("subject"))

Merge train and test data by combining usages of rbind and cbind.

uciData.train <- cbind(uciData.train.x, uciData.train.y, uciData.train.sub)
uciData.test  <- cbind(uciData.test.x, uciData.test.y, uciData.test.sub)
uciData.all <- rbind(uciData.train, uciData.test)

(Optional) Destroy redundant big datasets.

remove(uciData.train)
remove(uciData.test)
remove(uciData.train.x)
remove(uciData.test.x)
remove(uciData.train.y)
remove(uciData.test.y)
remove(uciData.train.sub)
remove(uciData.test.sub)

Select measurements on mean and standard deviation.

setnames(uciData.features, names(uciData.features), c('featureId', 'featureName'))
featuresOnMeanOrStd <- uciData.features[grepl("mean\\(\\)|std\\(\\)", uciData.features$featureName),]
featureIdsOnMeanOrStd <- c(paste('V', featuresOnMeanOrStd$featureId, sep=''), 'activityId', 'subject')
uciData.all <- uciData.all[, featureIdsOnMeanOrStd, with = F]

Set descriptive names.

uciData.activityNames <- uci.helpers.readAsDataTable(file.path(uci.datasetPath, 'activity_labels.txt'))
setnames(uciData.activityNames, names(uciData.activityNames), c("activityId", "activityName"))
setnames(uciData.all, paste0('V',featuresOnMeanOrStd$featureId), as.character(featuresOnMeanOrStd$featureName))

Write tidy dataset to file.

write.table(uciData.all, file="dataset.txt", row.names = F)

To generate means for very variables on each subject and each activity, we first devide uciData.all into sub tables group by activityId and subject.

uciData.all$activityId <- factor(uciData.all$activityId)
uciData.all$subject <- factor(uciData.all$subject)
uciData.grouped <- split(uciData.all, list(uciData.all$activityId, uciData.all$subject))

Then calculate colMeans for each value in list uciData.grouped.

uciData.summaryMean <- list()
for (item in uciData.grouped) {
  item$subject <- as.numeric(item$subject)
  item$activityId <- as.numeric(item$activityId)
  uciData.summaryMean <- c(uciData.summaryMean, colMeans(item))
}
uciData.summaryMean <- matrix(
  unlist(uciData.summaryMean),
  ncol = nrow(featuresOnMeanOrStd) + 2,
  byrow = T)
colnames(uciData.summaryMean) <- colnames(uciData.grouped$`1.1`)

Finally, write this dataset to file as well.

uci_har_analysis's People

Contributors

tjwudi avatar

Watchers

James Cloos avatar  avatar

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.