subject <- read.table("UCI HAR Dataset/test/subject_test.txt")
colnames(subject) <- c("subject")
activity <- read.table("UCI HAR Dataset/test/y_test.txt")
colnames(activity) <- c("activity")
activity <- factor(activity$activity, levels = c(1, 2, 3, 4, 5, 6), labels = c("walking", "walking_upstairs", "walking_downstairs", "sitting", "standing", "laying"))
dat1 <- cbind(subject, activity)
X_test <- read.table("UCI HAR Dataset/test/X_test.txt")
features <- read.table("UCI HAR Dataset/features.txt")
rows should be columns, so we will transpose rows and columns, and get just the names of the features
feature_name <- t(features)[2,]
colnames(X_test) <- feature_name
g_mean <- grep("mean()", feature_name)
g_std <- grep("std()", feature_name)
g_all <- append(g_mean, g_std)
sorted_indices <- sort(g_all)
X_test_mean_and_std <- X_test[, sorted_indices]
X_test_merged <- cbind(dat1, X_test_mean_and_std)
subjectTrain <- read.table("UCI HAR Dataset/train/subject_train.txt")
colnames(subjectTrain) <- c("subject")
activityTrain <- read.table("UCI HAR Dataset/train/y_train.txt")
colnames(activityTrain) <- c("activity")
activityTrain <- factor(activityTrain$activity, levels = c(1, 2, 3, 4, 5, 6), labels = c("walking", "walking_upstairs", "walking_downstairs", "sitting", "standing", "laying"))
dat2 <- cbind(subjectTrain, activityTrain)
colnames(dat2) <- c("subject", "activity")
X_train <- read.table("UCI HAR Dataset/train/X_train.txt")
colnames(X_train) <- feature_name
X_train_mean_and_std <- X_train[, sorted_indices]
X_train_merged <- cbind(dat2, X_train_mean_and_std)
X_final <- rbind(X_test_merged, X_train_merged)
X_tidy <- data.frame()
data_split <- split(X_final[, 3:81], list(X_final$subject, X_final$activity))
X_tidy <- sapply(data_split, colMeans)
write.table(X_tidy, file="./X_tidy.txt", sep="\t", row.names = FALSE)
write.table(X_tidy, file="./X_tidy_with_row_names.txt", sep="\t")
head(X_tidy, 2)