<repo_name>chatard/Coursera-Data-Cleaning-Project<file_sep>/README.md
---
title: "Cleaning Data Course Project: Tidy dataset."
author: "Jacques.Chatard"
date: "25 février 2018"
output: html_document
---

this work is part of the assignment concerning the chapter "Getting and Cleaning Data"
of the Johns Hopkins University Data Science course on coursera.org.

<ins>The original document that contains the base files can be found on:</ins>

[UCI.EDU](http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones)

* Original data comes from : UCI Machine Learning Repository.

* Link (below) for original zipped data files is:

[zipped data files](https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip)

I have downloaded this zip file in my working directory on february 18th 2018.
in my working directory unzipped files stand in a new directory named : "UCI HAR Dataset".

## The purpose of this assignment is:  


.1 Merges the training and the test sets to create one data set.

.2 Extracts only the measurements on the mean and standard deviation for each measurement.

.3 Uses descriptive activity names to name the activities in the data set

.4 Appropriately labels the data set with descriptive variable names.

.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.

## File structure in this document:

[README.md](https://github.com/chatard/Coursera-Data-Cleaning-Project/blob/master/README.md)

[run_analysis.R](https://github.com/chatard/Coursera-Data-Cleaning-Project/blob/master/run_analysis.R)

[CODEBOOK](https://github.com/chatard/Coursera-Data-Cleaning-Project/blob/master/CODEBOOK.md)

[New tidy dataset](https://github.com/chatard/Coursera-Data-Cleaning-Project/blob/master/tidydatasetG.txt)









<file_sep>/CODEBOOK.md
---
title: "CODEBOOK"
author: "john"
date: "27 février 2018"
output: html_document
---

## Codebook elements provided in the original files:

* ./UCI_HAR_Dataset/features_info.txt

this file lists and gives information on the main measured variables.  

* ./UCI_HAR_Dataset/features.txt

this file provides the names of each one of 561 measured or estimated variables.

* ./UCI_HAR_Dataset/activity_labels.txt

this file provides the names of the different categories of activities for which measurements have been made.

* ./UCI_HAR_Dataset/train/y_train.txt and ./UCI_HAR_Dataset/test/y_test.txt

these 2 files provide an identification code in range [1, 30] to identify each trial participant for each observation.


## About variables related units of measure:

* <ins>For acceleration type variables along an axis:</ins> units of gravity expressed in g.  

* <ins>For other variables (gyroscope angular speed):</ins> unit = gradians / second.


## Modified or added variables during the process for my "tidy data set":


 
 ### modified activity labels (line 32 of run_analysis.R) :  
 
 WALKING, WALKING_UPSTAIRS, WALKING_DOWNSTAIRS, SITTING, STANDING, LAYING. 
 
 respectively substituted by:
 
 walking, walkingupstairs, walkingdownstairs, sitting, laying.
 
### added : "subjid"
 
 * subjid is a subject identifier : line 16 in file run_analysis.R
 
### added : "activitycode" 
 
 * activitycode is a container for activity labels  : line 27 in file run_analysis.R
 
 
 
                    
 
                   <file_sep>/run_analysis.R
#run_analysis.R
# loading the libraries that I will use:
library(data.table)
library(dplyr)

# 1) Merges the training and the test sets to create one data set.
trainsubjects<-read.table("./UCI HAR Dataset/train/subject_train.txt")
testsubjects<-read.table("./UCI HAR Dataset/test/subject_test.txt")
# allsubjects will be the merged table of subjects in training sample and in test sample:
# using rbind() to create the merged table "allsubjects":
# this new object is a data table with 1 variable that contains the identifiers of the subjects 
# who participated in the experiment:
# I chose to give a more meaningful name to this variable, namely: subjid.
# for more details about the choice of variable names: please see in  my "codebook" file.
allsubjects<-data.table(rbind(trainsubjects,testsubjects))
allsubjects <- rename(allsubjects, subjid=V1)
# ..............................................................................
# 2) in the same way, "allactivities" will be a merged table of activities 
# in training sample and in test sample : 
# here, the variable "activitycode" designates the different phases of physical 
# activities that gave rise to measurements.
# this variable : "activitycode" may have six different values according to the 
# description of the original file "activity_labels.txt"        ....
trainactivities<-data.table(read.table("./UCI HAR Dataset/train/y_train.txt"))
testactivities <- data.table(read.table("./UCI HAR Dataset/test/y_test.txt"))
allactivities<-data.table(rbind(trainactivities,testactivities))
allactivities<-rename(allactivities, activitycode=V1)
# naming activities levels in the data set:
# for more details about the choice of variable names: please see in  my "codebook" file.
allactivities$activitycode<-factor(allactivities$activitycode,
    levels=c(1:6),
    labels=c("walking","walkingupstairs", "walkingdownstairs", "sitting", "standing", "laying"))
# ------------------------------------------------------------------- #

features<-read.table(file = "./UCI HAR Dataset/features.txt")
trainset <- data.table(read.table("./UCI HAR Dataset/train/X_train.txt"))
testset<- data.table(read.table(file = "./UCI HAR Dataset/test/X_test.txt"))
allset <- data.table(rbind(trainset,testset)) 
#featuressubset<-grep("mean()|std()", features[,2])
#featuressubset<-grep("mean()|std()|Mean", features[,2])
featuressubset<-grep("mean[^Freq]()|std()", features[,2])
meanandstdsubset<-allset[,..featuressubset]
subfea<-features[featuressubset,]
namesvec <- as.character(subfea$V2)
meanandstdsubset <- setnames(meanandstdsubset, namesvec)

#Creating a grouped tidydataset : step4:
# data set is already labeled with descriptive variable names...
# ... because labels added during each step.
tidydataset<-cbind(allsubjects, allactivities, meanandstdsubset)

#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.
#tidyarr<-arrange(tidydataset, activitycode, subjid)
#newtidydata<-melt(tidydataset, id.vars = c("subjid", "activitycode"))
#aqw <-dcast(newtidydata, formula = subjid +activitycode ~variable)
# ds<-group_by(tidydataset, activitycode,subjid)
# ds.avg<-summarise_all(ds,funs(mean))
tidydatasetG<-group_by(tidydataset, activitycode,subjid)
tidydatasetG<-summarise_all(tidydatasetG, funs(mean))

write.table(tidydatasetG, "tidydatasetG.txt", row.names = FALSE)
