-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_analysis.R
51 lines (41 loc) · 2.06 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
#load library
library(data.table)
#read data
features <- read.csv('features.txt', header = FALSE,sep = ' ')
features <- as.character(features[,2])
#convert data
data.train.x <- read.table('train/X_train.txt')
data.train.activity <- read.csv('train/y_train.txt',
header = FALSE, sep = ' ')
data.train.subject <- read.csv('train/subject_train.txt',header = FALSE, sep = ' ')
data.train <- data.frame(data.train.subject, data.train.activity, data.train.x)
names(data.train) <- c(c('subject', 'activity'), features)
data.test.x <- read.table('test/X_test.txt')
data.test.activity <- read.csv('test/y_test.txt',
header = FALSE, sep = ' ')
data.test.subject <- read.csv('test/subject_test.txt', header = FALSE, sep = ' ')
data.test <- data.frame(data.test.subject, data.test.activity, data.test.x)
names(data.test) <- c(c('subject', 'activity'), features)
data.all <- rbind(data.train, data.test)
#measurements on the mean and standard deviation for each measurement
mean_std.select <- grep('mean|std', features)
data.sub <- data.all[,c(1,2,mean_std.select + 2)]
#descriptive activity names to name the activities
activity.labels <- read.table('activity_labels.txt', header = FALSE)
activity.labels <- as.character(activity.labels[,2])
data.sub$activity <- activity.labels[data.sub$activity]
# labels the data set with descriptive variable names
name.new <- names(data.sub)
name.new <- gsub("[(][)]", "", name.new)
name.new <- gsub("^t", "TimeDomain_", name.new)
name.new <- gsub("^f", "FrequencyDomain_", name.new)
name.new <- gsub("Acc", "Accelerometer", name.new)
name.new <- gsub("Gyro", "Gyroscope", name.new)
name.new <- gsub("Mag", "Magnitude", name.new)
name.new <- gsub("-mean-", "_Mean_", name.new)
name.new <- gsub("-std-", "_StandardDeviation_", name.new)
name.new <- gsub("-", "_", name.new)
names(data.sub) <- name.new
#create tidy dataset
data.tidy <- aggregate(data.sub[,3:81], by = list(activity = data.sub$activity, subject = data.sub$subject),FUN = mean)
write.table(x = data.tidy, file = "data_tidy.txt", row.names = FALSE)