-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
80 lines (52 loc) · 2.49 KB
/
run_analysis.R
File metadata and controls
80 lines (52 loc) · 2.49 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
dostuff <- function()
{
featuresFN <- "UCI HAR Dataset/features.txt"
xTrainFN <- "UCI HAR Dataset/train/X_train.txt"
xTestFN <- "UCI HAR Dataset/test/X_test.txt"
subjectTrainFN <- "UCI HAR Dataset/train/subject_train.txt"
subjectTestFN <- "UCI HAR Dataset/test/subject_test.txt"
yTrainFN <- "UCI HAR Dataset/train/Y_train.txt"
yTestFN <- "UCI HAR Dataset/test/Y_test.txt"
activiyLabelsFN <- "UCI HAR Dataset/activity_labels.txt"
################################################################
featuresDF <- read.csv(featuresFN, header=F, sep=" ")
xTrainDF <- read.table(xTrainFN)
xTestDF <- read.table(xTestFN)
subjectTrainDF <- read.table(subjectTrainFN)
subjectTestDF <- read.table(subjectTestFN)
yTrainDF <- read.table(yTrainFN)
yTestDF <- read.table(yTestFN)
activityLabelsDF <- read.table(activiyLabelsFN)
################################################################
# merge train and test sets
mergedXDF <- rbind(xTrainDF, xTestDF)
colnames(mergedXDF) <- featuresDF[,2]
################################################################
# Extracts only the measurements on the mean and standard deviation for each measurement.
meanSdIDX <-grep("mean|std", colnames(mergedXDF))
meanSDDF <- mergedXDF[,meanSdIDX]
################################################################
# Uses descriptive activity names to name the activities in the data set
mergedYDF <- rbind(yTrainDF, yTestDF)
activityDF <- cbind(mergedYDF, meanSDDF)
colnames(activityDF)[1] <- "Activity"
################################################################
# Appropriately labels the data set with descriptive variable names.
activityLabelsDF[,2] <- as.character(activityLabelsDF[,2])
for(i in 1:length(activityDF[,1]))
{
activityDF[i,1] <- activityLabelsDF[activityDF[i,1],2]
}
################################################################
# creates a second, independent tidy data set with the average of each variable for each activity and each subject.
subjectsDF <- rbind(subjectTrainDF,subjectTestDF)
dataDF <- cbind(subjectsDF, activityDF)
colnames(dataDF)[1] <- "Subject"
tidyDF <- aggregate(dataDF[,3] ~ Subject+Activity, data=dataDF, FUN="mean")
for(i in 4:ncol(dataDF))
{
tidyDF[,i] <- aggregate( dataDF[,i] ~ Subject+Activity, data=dataDF, FUN="mean")[,3]
}
colnames(tidyDF)[3:ncol(tidyDF)] <- colnames(meanSDDF)
write.table(tidyDF, file="ExtractedData.txt", row.name=F)
}