-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
66 lines (39 loc) · 2.37 KB
/
Copy pathrun_analysis.R
File metadata and controls
66 lines (39 loc) · 2.37 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
UCI_HAR_Dataset <- file.path("C:/--Coursera/projs" , "UCI HAR Dataset")
files<-list.files(UCI_HAR_Dataset, recursive=TRUE)
# Read data
features_test_data <- read.table(file.path(UCI_HAR_Dataset, "test" , "X_test.txt" ),header = FALSE)
activity_test_data <- read.table(file.path(UCI_HAR_Dataset, "test" , "Y_test.txt" ),header = FALSE)
subject_test_data <- read.table(file.path(UCI_HAR_Dataset, "test" , "subject_test.txt"),header = FALSE)
features_train_data <- read.table(file.path(UCI_HAR_Dataset, "train", "X_train.txt"),header = FALSE)
activity_train_data <- read.table(file.path(UCI_HAR_Dataset, "train", "Y_train.txt"),header = FALSE)
subject_train_data <- read.table(file.path(UCI_HAR_Dataset, "train", "subject_train.txt"),header = FALSE)
# Merge train & test for each dataset
features_data <- rbind(features_train_data, features_test_data)
subject_data <- rbind(subject_train_data, subject_test_data)
activity_data <- rbind(activity_train_data, activity_test_data)
# Set names to variables
features_names <- read.table(file.path(UCI_HAR_Dataset, "features.txt"),head=FALSE)
names(features_data) <- features_names$V2
names(subject_data) <-c("subject")
names(activity_data) <- c("activity")
# Merge all data sets
all_data_set <- cbind( features_data, subject_data, activity_data)
# Get columns with mean() or std()
subset_features_names<-features_names$V2[grep("mean\\(\\)|std\\(\\)", features_names$V2)]
# Subset names of features
selected_names<-c(as.character(subset_features_names), "subject", "activity" )
all_data_set <- subset(all_data_set ,select=selected_names)
# Use descriptive activity names
activity_labels <- read.table(file.path(UCI_HAR_Dataset, "activity_labels.txt"),header = FALSE)
# label all data set with descriptive variable names
names(all_data_set)<-gsub("Acc", "Accelerometer", names(all_data_set))
names(all_data_set)<-gsub("Gyro", "Gyroscope", names(all_data_set))
names(all_data_set)<-gsub("Mag", "Magnitude", names(all_data_set))
names(all_data_set)<-gsub("BodyBody", "Body", names(all_data_set))
names(all_data_set)<-gsub("^t", "time", names(all_data_set))
names(all_data_set)<-gsub("^f", "frequency", names(all_data_set))
# Write tidy data set
library(plyr);
tidy_data<-aggregate(. ~subject + activity, all_data_set, mean)
tidy_data<-tidy_data[order(tidy_data$subject,tidy_data$activity),]
write.table(tidy_data, file = "tidy_data.txt",row.name=FALSE)