-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
52 lines (40 loc) · 2.37 KB
/
run_analysis.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# ---- Assignment 1. ----
#Merges the training and the test sets to create one data set.
#Load and merge test set data
ytest <- read.table("./test/y_test.txt")
Xtest <- read.table("./test/X_test.txt")
subjecttest <- read.table("./test/subject_test.txt")
testset <- data.frame(ytest,subjecttest,Xtest)
#Load and merge train set data
ytrain <- read.table("./train/y_train.txt")
Xtrain <- read.table("./train/X_train.txt")
subjecttrain <- read.table("./train/subject_train.txt")
trainset <- data.frame(ytrain,subjecttrain,Xtrain)
#Merge test and train datasets into one
dataset <- rbind(testset,trainset)
# ---- Assignment 2. ----
#Extracts only the measurements on the mean and standard deviation for each measurement.
featListTable <- read.table("./features.txt",stringsAsFactors=FALSE)
meanId <- grepl("mean",featListTable[,2], ignore.case=TRUE)
stdId <- grepl("std",featListTable[,2], ignore.case=TRUE)
feat2BKept <- meanId | stdId
featNames = featListTable[feat2BKept,2]
feat2BKept <- c(TRUE,TRUE,feat2BKept) #Keep also the 1st and 2nd column (y and subject)
extractedDataset <- dataset[,feat2BKept]
# ---- Assignment 3. ----
#Uses descriptive activity names to name the activities in the data set
activityLabels <- read.table("./activity_labels.txt") #Read activity names table
extractedDataset <- merge(extractedDataset,activityLabels,by.x="V1",by.y="V1",sort=FALSE) #join dataset with action names data
extractedDataset[,1] <- extractedDataset[,ncol(extractedDataset)]
extractedDataset <- extractedDataset[,1:ncol(extractedDataset)-1] # substitute activities ids with names
# ---- Assignment 4. ----
#Appropriately labels the data set with descriptive variable names.
featuresLabels <- read.table("./features.txt",stringsAsFactors=FALSE) #Read activity names table
featuresLabels <- featuresLabels[feat2BKept,2] #Select only the labels we are interested in
featuresLabels <- featuresLabels[!is.na(featuresLabels)]
featuresLabels <- c("Activity","Subject",featuresLabels)
colnames(extractedDataset) <- featuresLabels
# ---- Assignment 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.
tidyDataset <- aggregate(extractedDataset[,3:ncol(extractedDataset)],by=list(Activity=extractedDataset$Activity,Subject=extractedDataset$Subject),FUN=mean,na.rm=TRUE)
write.table(tidyDataset,"./tidy.txt" , row.name=FALSE)