filename <- "getdata_dataset.zip"
if (!file.exists(filename)){
fileURL <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip "
download.file(fileURL, filename, method="curl")
}
if (!file.exists("UCI HAR Dataset")) {
unzip(filename)
}
testActivity <- read.table("UCI HAR Dataset/test/Y_test.txt" , header = FALSE)
trainActivity <- read.table("UCI HAR Dataset/train/Y_train.txt", header = FALSE)
testSubject <- read.table("UCI HAR Dataset/test/subject_test.txt", header = FALSE)
trainSubject <- read.table("UCI HAR Dataset/train/subject_train.txt", header = FALSE)
testFeatures <- read.table("UCI HAR Dataset/test/X_test.txt", header = FALSE)
trainFeatures <- read.table("UCI HAR Dataset/train/X_train.txt", header = FALSE)
labels <- read.table("UCI HAR Dataset/activity_labels.txt", header = FALSE)
featuresNames <- read.table("UCI HAR Dataset/features.txt", head=FALSE)
activity <- rbind(trainActivity, testActivity)
subject <- rbind(trainSubject, testSubject)
features <- rbind(trainFeatures, testFeatures)
activity$V1 <- factor(activity$V1, levels = as.integer(labels$V1), labels = labels$V2)
names(activity)<- c("activity")
names(subject)<-c("subject")
names(features)<- featuresNames$V2
meanstdev<-c(as.character(featuresNames$V2[grep("mean\\(\\)|std\\(\\)", featuresNames$V2)]))
featuresSubset<-subset(features,select=meanstdev)
subjectactivity <- cbind(subject, activity)
finaldataset <- cbind(featuresSubset, subjectactivity)
names(finaldataset)<-gsub("^t", "time", names(finaldataset))
names(finaldataset)<-gsub("^f", "frequency", names(finaldataset))
suppressWarnings(cleandata <- aggregate(finaldataset, by = list(finaldataset$subject, finaldataset$activity), FUN = mean))
colnames(cleandata)[1] <- "Subject"
names(cleandata)[2] <- "Activity"
cleandata <- cleandata[1:68]
write.table(cleandata, file = "tidydataset.txt", row.name = FALSE)