Repository navigation
Expand file tree
/
Copy pathcodebook.rmd
More file actions
120 lines (102 loc) · 7.06 KB
/
Copy pathcodebook.rmd
File metadata and controls
120 lines (102 loc) · 7.06 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "codebook"
author: "dawky"
date: "Friday, December 08, 2017"
output: word_document
---
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r}
setwd("D:/r/coursera/gettingcleandata")
library(dplyr)
#1
#combine the data,read the x_train,y_train data,subject_train data and combine them into train data, do the same work for the test data, and then put the data together
trainx <- read.table("./UCI HAR Dataset/train/X_train.txt")
trainy <- read.table("./UCI HAR Dataset/train/y_train.txt")
trainsubject <- read.table("./UCI HAR Dataset/train/subject_train.txt")
testx <- read.table("./UCI HAR Dataset/test/X_test.txt")
testy <- read.table("./UCI HAR Dataset/test/y_test.txt")
testsubject <- read.table("./UCI HAR Dataset/test/subject_test.txt")
train <- cbind(trainsubject, trainy, trainx)
test <- cbind(testsubject, testy, testx)
dat <- rbind(train, test)
#2
#find the mean and std, from the feature_info file, we know the "mean" and ¡°std" word appear in the variable name
#I firstly select the needed variable from the features.txt file, and find the position of these variables from the featurename(feature2), and then apply it to the original data
feature<- read.table("./UCI HAR Dataset/features.txt")
feature2 <- grep(("mean\\(\\)|std\\(\\)"), feature[, 2])
featurename<-feature[feature2,2]
dat2 <- dat[, c(1, 2, feature2+2)]
colnames(dat2)<-c("subject","activity",paste(featurename))
#3
#change the activity number with name,view the activity name, find the number of each activity, and replace them
activity<- read.table("./UCI HAR Dataset/activity_labels.txt")
View(activity)
dat2[,2][dat2[,2]== 1] <- "Walking"
dat2[,2][dat2[,2] == 2] <- "Walking Upstairs"
dat2[,2][dat2[,2] == 3] <- "Walking Downstairs"
dat2[,2][dat2[,2] == 4] <- "Sitting"
dat2[,2][dat2[,2] == 5] <- "Standing"
dat2[,2][dat2[,2] == 6] <- "Laying"
#4
#redo the step2, change the short word into their full name, rewrite the featurename, and apply it to the colname
#for example, from the feature_info.txt file, we know that Acc represent for Accelerator, Mag represent for Magnitude,and it's them same for "Gyro"and "Gyroscope","t"and "time","f"and "frequency"
#we change our featurename list, screen for the original word and replace it with full name, and apply it to the colname
featurename2<-featurename
featurename2<-gsub("Acc", "Accelerator", featurename2)
featurename2<-gsub("Mag", "Magnitude", featurename2)
featurename2<-gsub("Gyro", "Gyroscope", featurename2)
featurename2<-gsub("^t", "time", featurename2)
featurename2<-gsub("^f", "frequency", featurename2)
colnames(dat2)<-c("subject","activity",paste(featurename2))
#5
#aggregate each col by the two listed variable using group_by, and then export it to clean txt file
cleandata<-dat2%>%group_by(subject,activity)%>%summarise_each(funs(mean))
write.table(cleandata, file = "cleandata.txt", row.names = FALSE)
##summary and decription for the variables in the cleandata
##variable for the feature
# "timeBodyAccelerator-mean()-X" "timeBodyAccelerator-mean()-Y"
# "timeBodyAccelerator-mean()-Z" "timeBodyAccelerator-std()-X"
# "timeBodyAccelerator-std()-Y" "timeBodyAccelerator-std()-Z"
# "timeGravityAccelerator-mean()-X" "timeGravityAccelerator-mean()-Y"
# "timeGravityAccelerator-mean()-Z" "timeGravityAccelerator-std()-X"
# "timeGravityAccelerator-std()-Y" "timeGravityAccelerator-std()-Z"
# "timeBodyAcceleratorJerk-mean()-X" "timeBodyAcceleratorJerk-mean()-Y"
# "timeBodyAcceleratorJerk-mean()-Z" "timeBodyAcceleratorJerk-std()-X"
# "timeBodyAcceleratorJerk-std()-Y" "timeBodyAcceleratorJerk-std()-Z"
# "timeBodyGyroscope-mean()-X" "timeBodyGyroscope-mean()-Y"
# "timeBodyGyroscope-mean()-Z" "timeBodyGyroscope-std()-X"
# "timeBodyGyroscope-std()-Y" "timeBodyGyroscope-std()-Z"
# "timeBodyGyroscopeJerk-mean()-X" "timeBodyGyroscopeJerk-mean()-Y"
# "timeBodyGyroscopeJerk-mean()-Z" "timeBodyGyroscopeJerk-std()-X"
# "timeBodyGyroscopeJerk-std()-Y" "timeBodyGyroscopeJerk-std()-Z"
# "timeBodyAcceleratorMagnitude-mean()" "timeBodyAcceleratorMagnitude-std()"
# "timeGravityAcceleratorMagnitude-mean()" "timeGravityAcceleratorMagnitude-std()"
# "timeBodyAcceleratorJerkMagnitude-mean()" "timeBodyAcceleratorJerkMagnitude-std()"
# "timeBodyGyroscopeMagnitude-mean()" "timeBodyGyroscopeMagnitude-std()"
# "timeBodyGyroscopeJerkMagnitude-mean()" "timeBodyGyroscopeJerkMagnitude-std()"
# "frequencyBodyAccelerator-mean()-X" "frequencyBodyAccelerator-mean()-Y"
# "frequencyBodyAccelerator-mean()-Z" "frequencyBodyAccelerator-std()-X"
# "frequencyBodyAccelerator-std()-Y" "frequencyBodyAccelerator-std()-Z"
# "frequencyBodyAcceleratorJerk-mean()-X" "frequencyBodyAcceleratorJerk-mean()-Y"
# "frequencyBodyAcceleratorJerk-mean()-Z" "frequencyBodyAcceleratorJerk-std()-X"
# "frequencyBodyAcceleratorJerk-std()-Y" "frequencyBodyAcceleratorJerk-std()-Z"
# "frequencyBodyGyroscope-mean()-X" "frequencyBodyGyroscope-mean()-Y"
# "frequencyBodyGyroscope-mean()-Z" "frequencyBodyGyroscope-std()-X"
# "frequencyBodyGyroscope-std()-Y" "frequencyBodyGyroscope-std()-Z"
# "frequencyBodyAcceleratorMagnitude-mean()" "frequencyBodyAcceleratorMagnitude-std()"
# "frequencyBodyBodyAcceleratorJerkMagnitude-mean()" "frequencyBodyBodyAcceleratorJerkMagnitude-std()"
# "frequencyBodyBodyGyroscopeMagnitude-mean()" "frequencyBodyBodyGyroscopeMagnitude-std()"
# "frequencyBodyBodyGyroscopeJerkMagnitude-mean()" "frequencyBodyBodyGyroscopeJerkMagnitude-std()"
##subject:
#The participant ("subject") ID
##acticity:
#The label of the activity performed when the corresponding measurements were taken
#WALKING (1)
#WALKING_UPSTAIRS (2)
#WALKING_DOWNSTAIRS (3)
#SITTING (4)
#STANDING (5)
#LAYING (6)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.