===========
The full script can be found in run_analysis.R
After loading raw train and test data sets and merging them together, we got feature data set X.all and label vector y.all
Following are the steps.
- Load
code.book - Extract mean and standard measurements by first getting their indices from codebook by
grep()then extracting corresponding columns
2.1) Extract mean variables:
mean.var.codes <- grep(code.book$feature, pattern = "mean")
mean.ds <- X.all[ ,mean.var.codes]
2.2) Here I went ahead step 4 and rename variables in mean.ds, notice that we need to pass value = T to grep() to get values instead of indices.
mean.var.names <- grep(code.book$feature, pattern = "mean", value = T)
names(mean.ds) <- mean.var.names
2.3) Proceed similarly for standard deviation variables to get std.ds
2.4) Bind mean.ds and std.ds to get first.ds
Task 5. Creates a second, independent tidy data set with the average of each variable for each activity and each subject
- Get train and test ids from
train/subject_train.txtandtest/subject_test.txtand bind them together to get all subjectids. - Now I create a second data set where I pasted togethere subject ids, activity labels, mean and standard deviation variables (from
first.ds)
second.ds <- data.frame("subject.id" = ids$V1, "activity" = y.all$act, stringsAsFactors = F)
second.ds <- cbind(second.ds, first.ds)
- Get average of each variable for each activity and each subject:
- Define a function
getMean(col)to get avg for a given variable for each combination of activity and subject usingtapply(). Note that output ofgetMean()is a data frame.
- Define a function
getMean <- function(col) {
tmp.res <- tapply(second.ds[ ,col], INDEX = second.ds[ ,1:2], mean)
tmp.df <- as.data.frame(tmp.res)
res <- data.frame("variable" = names(second.ds)[col], "subject.id" = 1:nrow(tmp.df))
cbind(res, tmp.df)
}
- Apply the function
getMean()to all columns containing mean and standard variables ofsecond.ds(columns from 3 onwards). Here I usedldply()from package plyr (which is convenient for controlling various kinds of outputs and also has a decent option for estimating time to complete, especially important for large data sets).
require(plyr)
tidy.ds <- ldply(3:ncol(second.ds), getMean, .progress = "time")