-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetting and Cleaning Data
More file actions
48 lines (44 loc) · 1.79 KB
/
Getting and Cleaning Data
File metadata and controls
48 lines (44 loc) · 1.79 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
#Step 1: Merge the training and the test sets to
# create one data set.
setwd("e:/Program Files/R/UCI HAR Dataset")
DatTest=read.table("test/X_test.txt")
DatTrain=read.table("train/X_train.txt")
#read test data and train data
MergedDat=rbind(DatTrain,DatTest)
#merge them
SubjectTest=read.table("test/subject_test.txt")
SubjectTrain=read.table("train/subject_train.txt")
#read data of subjects
MergedSubject=rbind(SubjectTrain,SubjectTest)
#merge them
Population=cbind(MergedSubject,MergedDat)
#merge all the data above to create a new data set
#Step 2: Extracts only the measurements on the mean
# and standard deviation for each measurement.
Features=read.table("features.txt")
colnames(Features)=c("FeatureNum","FeatureName")
ExtractedFeatures=Features[grepl("mean\\(\\)|std\\(\\)",Features$FeatureName),]
ExtractedMeasurements=Population[,ExtractedFeatures$FeatureName]
#Step 3:Use descriptive activity names to name
# the activities in the data set.
TrainingL=read.table("train/y_train.txt")
TestingL=read.table("test/y_test.txt")
ActL=rbind(TrainingL,TestingL)
colnames(ActL)="Activity"
Population=cbind(Population,ActL)
Labels=read.table("activity_labels.txt")
Population$Activity=as.character(Population$Activity)
Labels[,2]=as.character(Labels[,2])
NumWalk=grep("1",Population$Activity)
Population$Activity[NumWalk]=Labels[1,2]
NumUpstairs=grep("2",Population$Activity)
Population$Activity[NumUpstairs]=Labels[2,2]
NumDownstairs=grep("3",Population$Activity)
Population$Activity[NumDownstairs]=Labels[3,2]
NumSit=grep("4",Population$Activity)
Population$Activity[NumSit]=Labels[4,2]
NumStand=grep("5",Population$Activity)
Population$Activity[NumStand]=Labels[5,2]
NumLie=grep("6",Population$Activity)
Population$Activity[NumLie]=Labels[6,2]
write.table(Population,file="Course Project.txt",row.name=F)