-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_analysis.R
More file actions
72 lines (51 loc) · 2.15 KB
/
run_analysis.R
File metadata and controls
72 lines (51 loc) · 2.15 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
### Prepare ####
library(reshape)
wd <- "C:\\Users\\ddunn\\Dropbox\\DD Cloud\\Courses\\Coursera - Getting and Cleaning Data\\UCI HAR Dataset"
### Read in files ####
setwd(paste0(wd,"\\test"))
test0 <- read.table(file="X_test.txt",header=FALSE,stringsAsFactors=FALSE)
testlab0 <- read.table(file="y_test.txt",header=FALSE,stringsAsFactors=FALSE)
setwd(paste0(wd,"\\train"))
train0 <- read.table(file="X_train.txt",header=FALSE,stringsAsFactors=FALSE)
trainlab0 <- read.table(file="y_train.txt",header=FALSE,stringsAsFactors=FALSE)
### Assemble into one dataset ####
names(testlab0) <- "Activity"
test1 <- cbind(test0,testlab0)
test1$Set <- "Test"
names(trainlab0) <- "Activity"
train1 <- cbind(train0,trainlab0)
train1$Set <- "Train"
full0 <- rbind(test1,train1)
full1 <- full0
full1$Activity <- as.character(full1$Activity)
full1$Set <- as.factor(full1$Set)
### Read in feature labels ####
setwd(wd)
feat0 <- read.table(file="features.txt",header=FALSE,stringsAsFactors=FALSE)
feat1 <- feat0[,2]
### Condense to mean or sd variables ####
meansdlist <- grep("[Mm]ean|std",feat1)
### Label variables ####
colnames(full1) <- c(feat1,"Activity","Set")
full2 <- full1[,c(meansdlist,562:563)]
### Label activities ####
full2$Activity[full2$Activity=="1"] <- "Walking"
full2$Activity[full2$Activity=="2"] <- "Walking_Upstairs"
full2$Activity[full2$Activity=="3"] <- "Walking_Downstairs"
full2$Activity[full2$Activity=="4"] <- "Sitting"
full2$Activity[full2$Activity=="5"] <- "Standing"
full2$Activity[full2$Activity=="6"] <- "Lying"
### Include subjects ####
setwd(paste0(wd,"\\test"))
subtest0 <- read.table(file="subject_test.txt",header=FALSE,stringsAsFactors=FALSE)
setwd(paste0(wd,"\\train"))
subtrain0 <- read.table(file="subject_train.txt",header=FALSE,stringsAsFactors=FALSE)
subs0 <- data.frame(rbind(subtest0,subtrain0))
colnames(subs0) <- "Subject"
full3 <- cbind(full2,subs0)
### Summarize to average for each Activity/Subject ####
melt0 <- melt(full3,id.vars=c("Activity","Subject","Set"))
cast0 <- cast(melt0,Activity+Subject+Set~variable,mean)
#### Write final tidy data set ####
setwd(wd)
write.table(cast0,"tidydata.txt",sep="\t",row.names=FALSE)