Getting and Cleaning Data course project by George Li.
One of the most exciting areas in all of data science right now is wearable computing - see for example this article . Companies like Fitbit, Nike, and Jawbone Up are racing to develop the most advanced algorithms to attract new users. The data linked to from the course website represent data collected from the accelerometers from the Samsung Galaxy S smartphone. A full description is available at the site where the data was obtained:
http://archive.ics.uci.edu/ml/datasets/Human+Activity+Recognition+Using+Smartphones
Here are the data for the project:
https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip
You should create one R script called run_analysis.R that does the following. - Merges the training and the test sets to create one data set. - Extracts only the measurements on the mean and standard deviation for each measurement. - Uses descriptive activity names to name the activities in the data set - Appropriately labels the data set with descriptive variable names. - From the data set in step 4, creates a second, independent tidy data set with the average of each variable for each activity and each subject.
# Load the data
data_source <- "https://d396qusza40orc.cloudfront.net/getdata%2Fprojectfiles%2FUCI%20HAR%20Dataset.zip"
save_path <- "./data/UCI_HAR_Dataset.zip"
# Create the data directory
if (!file.exists("./data")) {
dir.create("./data")
}
# Download the data if it does not exist
if (!file.exists(save_path)) {
download.file(data_source, save_path)
}
# Unzip the data to the data directory
unzip(save_path, exdir = "./data")
# List the files in the data directory
list.files("./data/UCI HAR Dataset")
## [1] "activity_labels.txt" "features_info.txt" "features.txt"
## [4] "README.txt" "test" "train"
Then we load the data.
# Load the data into R, data are strings seperated by spaces
train <- read.table("./data/UCI HAR Dataset/train/X_train.txt")
test <- read.table("./data/UCI HAR Dataset/test/X_test.txt")
# Load the y data
train_y <- read.table("./data/UCI HAR Dataset/train/y_train.txt")
test_y <- read.table("./data/UCI HAR Dataset/test/y_test.txt")
# Load the subject data
train_subject <- read.table("./data/UCI HAR Dataset/train/subject_train.txt")
test_subject <- read.table("./data/UCI HAR Dataset/test/subject_test.txt")
# Load the activity labels
activity_labels <- read.table("./data/UCI HAR Dataset/activity_labels.txt")
# Load the feature labels
features <- read.table("./data/UCI HAR Dataset/features.txt")
Let’s take a look at the data to see what we have.
print(length(train)) # number of columns is 561
## [1] 561
print(nrow(train)) # train and train_y should have the same number of rows
## [1] 7352
print(nrow(train_y)) # number of rows is 7352
## [1] 7352
print(length(test)) # number of columns is 561
## [1] 561
print(nrow(test)) # test and test_y should have the same number of rows
## [1] 2947
print(nrow(test_y)) # number of rows is 2947
## [1] 2947
print(activity_labels) # 6 rows, 2 columns
## V1 V2
## 1 1 WALKING
## 2 2 WALKING_UPSTAIRS
## 3 3 WALKING_DOWNSTAIRS
## 4 4 SITTING
## 5 5 STANDING
## 6 6 LAYING
Now let’s do some merging to make one big data set.
# Merge X_train and y_train
train <- cbind(train, train_y) # add the y data to the training data, is 562 columns
test <- cbind(test, test_y) # add the y data to the test data, is 562 columns
# Merge the subject data
train <- cbind(train, train_subject) # add the subject data to the training data, is 563 columns
test <- cbind(test, test_subject) # add the subject data to the test data, is 563 columns
# Merge the training and test sets
data <- rbind(train, test)
print(length(data)) # should be the same as length(train) + 2 (for the y data and the subject data)
## [1] 563
print(nrow(data)) # should be the same as nrow(train) + nrow(test)
## [1] 10299
We see that the new total number of rows is 10299, or 7352 + 2947. The total number of columns is 563, or 561 + 1 (for the y data) + 1 (for the subject data).
Next, let’s get the measurements on the mean and standard deviation for each measurement.
# Get the indices of the columns that contain the mean and standard deviation
mean_std_indices <- grep("mean|std", features$V2)
print(mean_std_indices)
## [1] 1 2 3 4 5 6 41 42 43 44 45 46 81 82 83 84 85 86 121
## [20] 122 123 124 125 126 161 162 163 164 165 166 201 202 214 215 227 228 240 241
## [39] 253 254 266 267 268 269 270 271 294 295 296 345 346 347 348 349 350 373 374
## [58] 375 424 425 426 427 428 429 452 453 454 503 504 513 516 517 526 529 530 539
## [77] 542 543 552
print(length(mean_std_indices)) # 79 columns
## [1] 79
There are 79 columns that contain the mean and standard deviation. We can use these indices to subset the data.
Appropriately label the data set with descriptive variable names
# Get the names of the columns that contain the mean and standard deviation
mean_std_names <- features$V2[mean_std_indices]
print(mean_std_names)
## [1] "tBodyAcc-mean()-X" "tBodyAcc-mean()-Y"
## [3] "tBodyAcc-mean()-Z" "tBodyAcc-std()-X"
## [5] "tBodyAcc-std()-Y" "tBodyAcc-std()-Z"
## [7] "tGravityAcc-mean()-X" "tGravityAcc-mean()-Y"
## [9] "tGravityAcc-mean()-Z" "tGravityAcc-std()-X"
## [11] "tGravityAcc-std()-Y" "tGravityAcc-std()-Z"
## [13] "tBodyAccJerk-mean()-X" "tBodyAccJerk-mean()-Y"
## [15] "tBodyAccJerk-mean()-Z" "tBodyAccJerk-std()-X"
## [17] "tBodyAccJerk-std()-Y" "tBodyAccJerk-std()-Z"
## [19] "tBodyGyro-mean()-X" "tBodyGyro-mean()-Y"
## [21] "tBodyGyro-mean()-Z" "tBodyGyro-std()-X"
## [23] "tBodyGyro-std()-Y" "tBodyGyro-std()-Z"
## [25] "tBodyGyroJerk-mean()-X" "tBodyGyroJerk-mean()-Y"
## [27] "tBodyGyroJerk-mean()-Z" "tBodyGyroJerk-std()-X"
## [29] "tBodyGyroJerk-std()-Y" "tBodyGyroJerk-std()-Z"
## [31] "tBodyAccMag-mean()" "tBodyAccMag-std()"
## [33] "tGravityAccMag-mean()" "tGravityAccMag-std()"
## [35] "tBodyAccJerkMag-mean()" "tBodyAccJerkMag-std()"
## [37] "tBodyGyroMag-mean()" "tBodyGyroMag-std()"
## [39] "tBodyGyroJerkMag-mean()" "tBodyGyroJerkMag-std()"
## [41] "fBodyAcc-mean()-X" "fBodyAcc-mean()-Y"
## [43] "fBodyAcc-mean()-Z" "fBodyAcc-std()-X"
## [45] "fBodyAcc-std()-Y" "fBodyAcc-std()-Z"
## [47] "fBodyAcc-meanFreq()-X" "fBodyAcc-meanFreq()-Y"
## [49] "fBodyAcc-meanFreq()-Z" "fBodyAccJerk-mean()-X"
## [51] "fBodyAccJerk-mean()-Y" "fBodyAccJerk-mean()-Z"
## [53] "fBodyAccJerk-std()-X" "fBodyAccJerk-std()-Y"
## [55] "fBodyAccJerk-std()-Z" "fBodyAccJerk-meanFreq()-X"
## [57] "fBodyAccJerk-meanFreq()-Y" "fBodyAccJerk-meanFreq()-Z"
## [59] "fBodyGyro-mean()-X" "fBodyGyro-mean()-Y"
## [61] "fBodyGyro-mean()-Z" "fBodyGyro-std()-X"
## [63] "fBodyGyro-std()-Y" "fBodyGyro-std()-Z"
## [65] "fBodyGyro-meanFreq()-X" "fBodyGyro-meanFreq()-Y"
## [67] "fBodyGyro-meanFreq()-Z" "fBodyAccMag-mean()"
## [69] "fBodyAccMag-std()" "fBodyAccMag-meanFreq()"
## [71] "fBodyBodyAccJerkMag-mean()" "fBodyBodyAccJerkMag-std()"
## [73] "fBodyBodyAccJerkMag-meanFreq()" "fBodyBodyGyroMag-mean()"
## [75] "fBodyBodyGyroMag-std()" "fBodyBodyGyroMag-meanFreq()"
## [77] "fBodyBodyGyroJerkMag-mean()" "fBodyBodyGyroJerkMag-std()"
## [79] "fBodyBodyGyroJerkMag-meanFreq()"
# Get a subset of data that contains only the mean and standard deviation columns
data_subset <- data[, c(mean_std_indices, 562, 563)]
# Name the columns of the subset data
names(data_subset) <- c(mean_std_names, "activity", "subject")
print(head(data_subset))
## tBodyAcc-mean()-X tBodyAcc-mean()-Y tBodyAcc-mean()-Z tBodyAcc-std()-X
## 1 0.2885845 -0.02029417 -0.1329051 -0.9952786
## 2 0.2784188 -0.01641057 -0.1235202 -0.9982453
## 3 0.2796531 -0.01946716 -0.1134617 -0.9953796
## 4 0.2791739 -0.02620065 -0.1232826 -0.9960915
## 5 0.2766288 -0.01656965 -0.1153619 -0.9981386
## 6 0.2771988 -0.01009785 -0.1051373 -0.9973350
## tBodyAcc-std()-Y tBodyAcc-std()-Z tGravityAcc-mean()-X tGravityAcc-mean()-Y
## 1 -0.9831106 -0.9135264 0.9633961 -0.1408397
## 2 -0.9753002 -0.9603220 0.9665611 -0.1415513
## 3 -0.9671870 -0.9789440 0.9668781 -0.1420098
## 4 -0.9834027 -0.9906751 0.9676152 -0.1439765
## 5 -0.9808173 -0.9904816 0.9682244 -0.1487502
## 6 -0.9904868 -0.9954200 0.9679482 -0.1482100
## tGravityAcc-mean()-Z tGravityAcc-std()-X tGravityAcc-std()-Y
## 1 0.11537494 -0.9852497 -0.9817084
## 2 0.10937881 -0.9974113 -0.9894474
## 3 0.10188392 -0.9995740 -0.9928658
## 4 0.09985014 -0.9966456 -0.9813928
## 5 0.09448590 -0.9984293 -0.9880982
## 6 0.09190972 -0.9989793 -0.9867539
## tGravityAcc-std()-Z tBodyAccJerk-mean()-X tBodyAccJerk-mean()-Y
## 1 -0.8776250 0.07799634 0.005000803
## 2 -0.9316387 0.07400671 0.005771104
## 3 -0.9929172 0.07363596 0.003104037
## 4 -0.9784764 0.07732061 0.020057642
## 5 -0.9787449 0.07344436 0.019121574
## 6 -0.9973064 0.07793244 0.018684046
## tBodyAccJerk-mean()-Z tBodyAccJerk-std()-X tBodyAccJerk-std()-Y
## 1 -0.067830808 -0.9935191 -0.9883600
## 2 0.029376633 -0.9955481 -0.9810636
## 3 -0.009045631 -0.9907428 -0.9809556
## 4 -0.009864772 -0.9926974 -0.9875527
## 5 0.016779979 -0.9964202 -0.9883587
## 6 0.009344434 -0.9948136 -0.9887145
## tBodyAccJerk-std()-Z tBodyGyro-mean()-X tBodyGyro-mean()-Y tBodyGyro-mean()-Z
## 1 -0.9935750 -0.006100849 -0.03136479 0.10772540
## 2 -0.9918457 -0.016111620 -0.08389378 0.10058429
## 3 -0.9896866 -0.031698294 -0.10233542 0.09612688
## 4 -0.9934976 -0.043409983 -0.09138618 0.08553770
## 5 -0.9924549 -0.033960416 -0.07470803 0.07739203
## 6 -0.9922663 -0.028775508 -0.07039311 0.07901214
## tBodyGyro-std()-X tBodyGyro-std()-Y tBodyGyro-std()-Z tBodyGyroJerk-mean()-X
## 1 -0.9853103 -0.9766234 -0.9922053 -0.09916740
## 2 -0.9831200 -0.9890458 -0.9891212 -0.11050283
## 3 -0.9762921 -0.9935518 -0.9863787 -0.10848567
## 4 -0.9913848 -0.9924073 -0.9875542 -0.09116989
## 5 -0.9851836 -0.9923781 -0.9874019 -0.09077010
## 6 -0.9851808 -0.9921175 -0.9830768 -0.09424758
## tBodyGyroJerk-mean()-Y tBodyGyroJerk-mean()-Z tBodyGyroJerk-std()-X
## 1 -0.05551737 -0.06198580 -0.9921107
## 2 -0.04481873 -0.05924282 -0.9898726
## 3 -0.04241031 -0.05582883 -0.9884618
## 4 -0.03633262 -0.06046466 -0.9911194
## 5 -0.03763253 -0.05828932 -0.9913545
## 6 -0.04335526 -0.04193600 -0.9916216
## tBodyGyroJerk-std()-Y tBodyGyroJerk-std()-Z tBodyAccMag-mean()
## 1 -0.9925193 -0.9920553 -0.9594339
## 2 -0.9972926 -0.9938510 -0.9792892
## 3 -0.9956321 -0.9915318 -0.9837031
## 4 -0.9966410 -0.9933289 -0.9865418
## 5 -0.9964730 -0.9945110 -0.9928271
## 6 -0.9960147 -0.9930906 -0.9942950
## tBodyAccMag-std() tGravityAccMag-mean() tGravityAccMag-std()
## 1 -0.9505515 -0.9594339 -0.9505515
## 2 -0.9760571 -0.9792892 -0.9760571
## 3 -0.9880196 -0.9837031 -0.9880196
## 4 -0.9864213 -0.9865418 -0.9864213
## 5 -0.9912754 -0.9928271 -0.9912754
## 6 -0.9952490 -0.9942950 -0.9952490
## tBodyAccJerkMag-mean() tBodyAccJerkMag-std() tBodyGyroMag-mean()
## 1 -0.9933059 -0.9943364 -0.9689591
## 2 -0.9912535 -0.9916944 -0.9806831
## 3 -0.9885313 -0.9903969 -0.9763171
## 4 -0.9930780 -0.9933808 -0.9820599
## 5 -0.9934800 -0.9958537 -0.9852037
## 6 -0.9930177 -0.9954243 -0.9858944
## tBodyGyroMag-std() tBodyGyroJerkMag-mean() tBodyGyroJerkMag-std()
## 1 -0.9643352 -0.9942478 -0.9913676
## 2 -0.9837542 -0.9951232 -0.9961016
## 3 -0.9860515 -0.9934032 -0.9950910
## 4 -0.9873511 -0.9955022 -0.9952666
## 5 -0.9890626 -0.9958076 -0.9952580
## 6 -0.9864403 -0.9952748 -0.9952050
## fBodyAcc-mean()-X fBodyAcc-mean()-Y fBodyAcc-mean()-Z fBodyAcc-std()-X
## 1 -0.9947832 -0.9829841 -0.9392687 -0.9954217
## 2 -0.9974507 -0.9768517 -0.9735227 -0.9986803
## 3 -0.9935941 -0.9725115 -0.9833040 -0.9963128
## 4 -0.9954906 -0.9835697 -0.9910798 -0.9963121
## 5 -0.9972859 -0.9823010 -0.9883694 -0.9986065
## 6 -0.9966567 -0.9869395 -0.9927386 -0.9976438
## fBodyAcc-std()-Y fBodyAcc-std()-Z fBodyAcc-meanFreq()-X fBodyAcc-meanFreq()-Y
## 1 -0.9831330 -0.9061650 0.25248290 0.13183575
## 2 -0.9749298 -0.9554381 0.27130855 0.04286364
## 3 -0.9655059 -0.9770493 0.12453124 -0.06461056
## 4 -0.9832444 -0.9902291 0.02904438 0.08030227
## 5 -0.9801295 -0.9919150 0.18108977 0.05798789
## 6 -0.9922637 -0.9970459 0.15738377 0.31883523
## fBodyAcc-meanFreq()-Z fBodyAccJerk-mean()-X fBodyAccJerk-mean()-Y
## 1 -0.05205025 -0.9923325 -0.9871699
## 2 -0.01430976 -0.9950322 -0.9813115
## 3 0.08267692 -0.9909937 -0.9816423
## 4 0.18569468 -0.9944466 -0.9887272
## 5 0.55978632 -0.9962920 -0.9887900
## 6 0.60559943 -0.9948507 -0.9882443
## fBodyAccJerk-mean()-Z fBodyAccJerk-std()-X fBodyAccJerk-std()-Y
## 1 -0.9896961 -0.9958207 -0.9909363
## 2 -0.9897398 -0.9966523 -0.9820839
## 3 -0.9875663 -0.9912488 -0.9814148
## 4 -0.9913542 -0.9913783 -0.9869269
## 5 -0.9906244 -0.9969025 -0.9886067
## 6 -0.9901575 -0.9952180 -0.9901788
## fBodyAccJerk-std()-Z fBodyAccJerk-meanFreq()-X fBodyAccJerk-meanFreq()-Y
## 1 -0.9970517 0.87038451 0.21069700
## 2 -0.9926268 0.60851352 -0.05367561
## 3 -0.9904159 0.11543400 -0.19343634
## 4 -0.9943908 0.03579805 -0.09303585
## 5 -0.9929065 0.27335020 0.07913538
## 6 -0.9930667 0.32883589 0.05477140
## fBodyAccJerk-meanFreq()-Z fBodyGyro-mean()-X fBodyGyro-mean()-Y
## 1 0.26370789 -0.9865744 -0.9817615
## 2 0.06314827 -0.9773867 -0.9925300
## 3 0.03825433 -0.9754332 -0.9937147
## 4 0.16809523 -0.9871096 -0.9936015
## 5 0.29238418 -0.9824465 -0.9929838
## 6 0.32094497 -0.9848902 -0.9927862
## fBodyGyro-mean()-Z fBodyGyro-std()-X fBodyGyro-std()-Y fBodyGyro-std()-Z
## 1 -0.9895148 -0.9850326 -0.9738861 -0.9940349
## 2 -0.9896058 -0.9849043 -0.9871681 -0.9897847
## 3 -0.9867557 -0.9766422 -0.9933990 -0.9873282
## 4 -0.9871913 -0.9928104 -0.9916460 -0.9886776
## 5 -0.9886664 -0.9859818 -0.9919558 -0.9879443
## 6 -0.9807784 -0.9852871 -0.9916595 -0.9853661
## fBodyGyro-meanFreq()-X fBodyGyro-meanFreq()-Y fBodyGyro-meanFreq()-Z
## 1 -0.25754888 0.09794711 0.54715105
## 2 -0.04816744 -0.40160791 -0.06817833
## 3 -0.21668507 -0.01726417 -0.11072029
## 4 0.21686246 -0.13524536 -0.04972798
## 5 -0.15334258 -0.08840273 -0.16223039
## 6 -0.36303968 -0.13323831 0.19483324
## fBodyAccMag-mean() fBodyAccMag-std() fBodyAccMag-meanFreq()
## 1 -0.9521547 -0.9561340 -0.08843612
## 2 -0.9808566 -0.9758658 -0.04414989
## 3 -0.9877948 -0.9890155 0.25789914
## 4 -0.9875187 -0.9867420 0.07358150
## 5 -0.9935909 -0.9900635 0.39431033
## 6 -0.9948360 -0.9952833 0.43796212
## fBodyBodyAccJerkMag-mean() fBodyBodyAccJerkMag-std()
## 1 -0.9937257 -0.9937550
## 2 -0.9903355 -0.9919603
## 3 -0.9892801 -0.9908667
## 4 -0.9927689 -0.9916998
## 5 -0.9955228 -0.9943890
## 6 -0.9947329 -0.9951562
## fBodyBodyAccJerkMag-meanFreq() fBodyBodyGyroMag-mean() fBodyBodyGyroMag-std()
## 1 0.3469885 -0.9801349 -0.9613094
## 2 0.5320605 -0.9882956 -0.9833219
## 3 0.6607950 -0.9892548 -0.9860277
## 4 0.6789213 -0.9894128 -0.9878358
## 5 0.5590577 -0.9914330 -0.9890594
## 6 0.2469096 -0.9905000 -0.9858609
## fBodyBodyGyroMag-meanFreq() fBodyBodyGyroJerkMag-mean()
## 1 -0.1289889 -0.9919904
## 2 -0.2719585 -0.9958539
## 3 -0.2127279 -0.9950305
## 4 -0.0356842 -0.9952207
## 5 -0.2735820 -0.9950928
## 6 -0.2973291 -0.9951433
## fBodyBodyGyroJerkMag-std() fBodyBodyGyroJerkMag-meanFreq() activity subject
## 1 -0.9906975 -0.07432303 5 1
## 2 -0.9963995 0.15807454 5 1
## 3 -0.9951274 0.41450281 5 1
## 4 -0.9952369 0.40457253 5 1
## 5 -0.9954648 0.08775301 5 1
## 6 -0.9952387 0.01995331 5 1
Create a second, independent tidy data set with the average of each variable for each activity and each subject.
# Load the dplyr package
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# Group the data by activity and subject
grouped_data <- group_by(data_subset, activity, subject)
print(nrow(grouped_data)) # 10299
## [1] 10299
print(head(grouped_data))
## # A tibble: 6 × 81
## # Groups: activity, subject [1]
## `tBodyAcc-mean()-X` `tBodyAcc-mean()-Y` `tBodyAcc-mean()-Z` `tBodyAcc-std()-X`
## <dbl> <dbl> <dbl> <dbl>
## 1 0.289 -0.0203 -0.133 -0.995
## 2 0.278 -0.0164 -0.124 -0.998
## 3 0.280 -0.0195 -0.113 -0.995
## 4 0.279 -0.0262 -0.123 -0.996
## 5 0.277 -0.0166 -0.115 -0.998
## 6 0.277 -0.0101 -0.105 -0.997
## # ℹ 77 more variables: `tBodyAcc-std()-Y` <dbl>, `tBodyAcc-std()-Z` <dbl>,
## # `tGravityAcc-mean()-X` <dbl>, `tGravityAcc-mean()-Y` <dbl>,
## # `tGravityAcc-mean()-Z` <dbl>, `tGravityAcc-std()-X` <dbl>,
## # `tGravityAcc-std()-Y` <dbl>, `tGravityAcc-std()-Z` <dbl>,
## # `tBodyAccJerk-mean()-X` <dbl>, `tBodyAccJerk-mean()-Y` <dbl>,
## # `tBodyAccJerk-mean()-Z` <dbl>, `tBodyAccJerk-std()-X` <dbl>,
## # `tBodyAccJerk-std()-Y` <dbl>, `tBodyAccJerk-std()-Z` <dbl>, …
# Summarize the data by taking the mean of each variable for each activity and each subject
summarized_data <- summarise_all(grouped_data, mean)
print(nrow(summarized_data))
## [1] 180
print(head(summarized_data))
## # A tibble: 6 × 81
## # Groups: activity [1]
## activity subject `tBodyAcc-mean()-X` `tBodyAcc-mean()-Y` `tBodyAcc-mean()-Z`
## <int> <int> <dbl> <dbl> <dbl>
## 1 1 1 0.277 -0.0174 -0.111
## 2 1 2 0.276 -0.0186 -0.106
## 3 1 3 0.276 -0.0172 -0.113
## 4 1 4 0.279 -0.0148 -0.111
## 5 1 5 0.278 -0.0173 -0.108
## 6 1 6 0.284 -0.0169 -0.110
## # ℹ 76 more variables: `tBodyAcc-std()-X` <dbl>, `tBodyAcc-std()-Y` <dbl>,
## # `tBodyAcc-std()-Z` <dbl>, `tGravityAcc-mean()-X` <dbl>,
## # `tGravityAcc-mean()-Y` <dbl>, `tGravityAcc-mean()-Z` <dbl>,
## # `tGravityAcc-std()-X` <dbl>, `tGravityAcc-std()-Y` <dbl>,
## # `tGravityAcc-std()-Z` <dbl>, `tBodyAccJerk-mean()-X` <dbl>,
## # `tBodyAccJerk-mean()-Y` <dbl>, `tBodyAccJerk-mean()-Z` <dbl>,
## # `tBodyAccJerk-std()-X` <dbl>, `tBodyAccJerk-std()-Y` <dbl>, …
We see that after summarizing the data, we have 180 rows, or 30 subjects * 6 activities. The number of columns is 81, or 79 mean and standard deviation columns + 1 activity column + 1 subject column.
Finally, we write the data to a file for submission.
# Write the summarized data to a file
write.table(summarized_data, file = "tidy_data.txt", row.names = FALSE)
Here is how we can read the data back in R.
# Read the data back into R
tidy_data <- read.table("tidy_data.txt", header = TRUE)
print(head(tidy_data))
## activity subject tBodyAcc.mean...X tBodyAcc.mean...Y tBodyAcc.mean...Z
## 1 1 1 0.2773308 -0.01738382 -0.1111481
## 2 1 2 0.2764266 -0.01859492 -0.1055004
## 3 1 3 0.2755675 -0.01717678 -0.1126749
## 4 1 4 0.2785820 -0.01483995 -0.1114031
## 5 1 5 0.2778423 -0.01728503 -0.1077418
## 6 1 6 0.2836589 -0.01689542 -0.1103032
## tBodyAcc.std...X tBodyAcc.std...Y tBodyAcc.std...Z tGravityAcc.mean...X
## 1 -0.2837403 0.11446134 -0.2600279 0.9352232
## 2 -0.4236428 -0.07809125 -0.4252575 0.9130173
## 3 -0.3603567 -0.06991407 -0.3874120 0.9365067
## 4 -0.4408300 -0.07882674 -0.5862528 0.9639997
## 5 -0.2940985 0.07674840 -0.4570214 0.9726250
## 6 -0.2965387 0.16421388 -0.5043242 0.9580675
## tGravityAcc.mean...Y tGravityAcc.mean...Z tGravityAcc.std...X
## 1 -0.28216502 -0.068102864 -0.9766096
## 2 -0.34660709 0.084727087 -0.9726932
## 3 -0.26198636 -0.138107866 -0.9777716
## 4 -0.08585403 0.127764113 -0.9838265
## 5 -0.10044029 0.002476236 -0.9793484
## 6 -0.21469485 0.033188883 -0.9777799
## tGravityAcc.std...Y tGravityAcc.std...Z tBodyAccJerk.mean...X
## 1 -0.9713060 -0.9477172 0.07404163
## 2 -0.9721169 -0.9720728 0.06180807
## 3 -0.9623556 -0.9520918 0.08147459
## 4 -0.9679632 -0.9629681 0.07835291
## 5 -0.9615855 -0.9645808 0.08458888
## 6 -0.9642486 -0.9572050 0.06995859
## tBodyAccJerk.mean...Y tBodyAccJerk.mean...Z tBodyAccJerk.std...X
## 1 0.028272110 -4.168406e-03 -0.1136156
## 2 0.018249268 7.895337e-03 -0.2775305
## 3 0.010059149 -5.622646e-03 -0.2686796
## 4 0.002956024 -7.676793e-04 -0.2970426
## 5 -0.016319410 8.321594e-05 -0.3028910
## 6 -0.016483172 -7.389312e-03 -0.1327848
## tBodyAccJerk.std...Y tBodyAccJerk.std...Z tBodyGyro.mean...X
## 1 0.067002501 -0.5026998 -0.04183096
## 2 -0.016602236 -0.5860904 -0.05302582
## 3 -0.044961959 -0.5294861 -0.02564052
## 4 -0.221165132 -0.7513914 -0.03179826
## 5 -0.091039743 -0.6128953 -0.04889199
## 6 0.008088974 -0.5757775 -0.02550962
## tBodyGyro.mean...Y tBodyGyro.mean...Z tBodyGyro.std...X tBodyGyro.std...Y
## 1 -0.06953005 0.08494482 -0.4735355 -0.05460777
## 2 -0.04823823 0.08283366 -0.5615503 -0.53845367
## 3 -0.07791509 0.08134859 -0.5718696 -0.56379326
## 4 -0.07269053 0.08056772 -0.5009167 -0.66539409
## 5 -0.06901352 0.08154355 -0.4908775 -0.50462203
## 6 -0.07444625 0.08388088 -0.4460210 -0.33170227
## tBodyGyro.std...Z tBodyGyroJerk.mean...X tBodyGyroJerk.mean...Y
## 1 -0.3442666 -0.08999754 -0.03984287
## 2 -0.4810855 -0.08188334 -0.05382994
## 3 -0.4766964 -0.09523982 -0.03878747
## 4 -0.6626082 -0.11532156 -0.03934745
## 5 -0.3187006 -0.08884084 -0.04495595
## 6 -0.3831393 -0.08788911 -0.03623090
## tBodyGyroJerk.mean...Z tBodyGyroJerk.std...X tBodyGyroJerk.std...Y
## 1 -0.04613093 -0.2074219 -0.3044685
## 2 -0.05149392 -0.3895498 -0.6341404
## 3 -0.05036161 -0.3859230 -0.6390880
## 4 -0.05511669 -0.4923411 -0.8074199
## 5 -0.04826796 -0.3576814 -0.5714381
## 6 -0.05395973 -0.1826009 -0.4163902
## tBodyGyroJerk.std...Z tBodyAccMag.mean.. tBodyAccMag.std..
## 1 -0.4042555 -0.1369712 -0.2196886
## 2 -0.4354927 -0.2904076 -0.4225442
## 3 -0.5366641 -0.2546903 -0.3284289
## 4 -0.6404541 -0.3120506 -0.5276791
## 5 -0.1576825 -0.1583387 -0.3771787
## 6 -0.1666844 -0.1668407 -0.2667342
## tGravityAccMag.mean.. tGravityAccMag.std.. tBodyAccJerkMag.mean..
## 1 -0.1369712 -0.2196886 -0.1414288
## 2 -0.2904076 -0.4225442 -0.2814242
## 3 -0.2546903 -0.3284289 -0.2800093
## 4 -0.3120506 -0.5276791 -0.3667009
## 5 -0.1583387 -0.3771787 -0.2883330
## 6 -0.1668407 -0.2667342 -0.1951170
## tBodyAccJerkMag.std.. tBodyGyroMag.mean.. tBodyGyroMag.std..
## 1 -0.07447175 -0.1609796 -0.1869784
## 2 -0.16415099 -0.4465491 -0.5530199
## 3 -0.13991636 -0.4664118 -0.5615107
## 4 -0.31691896 -0.4977922 -0.5531161
## 5 -0.28224228 -0.3559331 -0.4921768
## 6 -0.07060296 -0.2812078 -0.3656029
## tBodyGyroJerkMag.mean.. tBodyGyroJerkMag.std.. fBodyAcc.mean...X
## 1 -0.2987037 -0.3253249 -0.2027943
## 2 -0.5479120 -0.5577982 -0.3460482
## 3 -0.5661352 -0.5673716 -0.3166140
## 4 -0.6813040 -0.7301464 -0.4267194
## 5 -0.4445325 -0.4891997 -0.2877826
## 6 -0.3212905 -0.3647083 -0.1879343
## fBodyAcc.mean...Y fBodyAcc.mean...Z fBodyAcc.std...X fBodyAcc.std...Y
## 1 0.089712726 -0.3315601 -0.3191347 0.05604001
## 2 -0.021904810 -0.4538064 -0.4576514 -0.16921969
## 3 -0.081302435 -0.4123741 -0.3792768 -0.12403083
## 4 -0.149399633 -0.6310055 -0.4472349 -0.10179945
## 5 0.009460378 -0.4902511 -0.2975174 0.04260268
## 6 0.140781622 -0.4985202 -0.3452277 0.10169964
## fBodyAcc.std...Z fBodyAcc.meanFreq...X fBodyAcc.meanFreq...Y
## 1 -0.2796868 -0.2075484 0.113093646
## 2 -0.4552221 -0.1457955 0.198586467
## 3 -0.4229985 -0.2466142 0.171743003
## 4 -0.5941983 -0.1385916 0.012347734
## 5 -0.4830600 -0.3223820 -0.002040511
## 6 -0.5504746 -0.1968417 0.022073419
## fBodyAcc.meanFreq...Z fBodyAccJerk.mean...X fBodyAccJerk.mean...Y
## 1 0.04972652 -0.1705470 -0.03522552
## 2 0.06889952 -0.3046153 -0.07876408
## 3 0.07485366 -0.3046944 -0.14050859
## 4 -0.07878926 -0.3588834 -0.27955339
## 5 0.02473897 -0.3449548 -0.18105555
## 6 0.18510704 -0.1509429 -0.07537423
## fBodyAccJerk.mean...Z fBodyAccJerk.std...X fBodyAccJerk.std...Y
## 1 -0.4689992 -0.1335866 0.106739857
## 2 -0.5549567 -0.3143131 -0.015332952
## 3 -0.5141373 -0.2965966 -0.005614988
## 4 -0.7289916 -0.2973261 -0.209900006
## 5 -0.5904966 -0.3213903 -0.054521360
## 6 -0.5414386 -0.1926947 0.031445068
## fBodyAccJerk.std...Z fBodyAccJerk.meanFreq...X fBodyAccJerk.meanFreq...Y
## 1 -0.5347134 -0.20926197 -0.3862371
## 2 -0.6158982 -0.07271016 -0.2635740
## 3 -0.5435291 -0.21604267 -0.2586680
## 4 -0.7723591 -0.13528039 -0.3858809
## 5 -0.6334300 -0.35937258 -0.5340498
## 6 -0.6086244 -0.17829518 -0.4663058
## fBodyAccJerk.meanFreq...Z fBodyGyro.mean...X fBodyGyro.mean...Y
## 1 -0.1855303 -0.3390322 -0.1030594
## 2 -0.2548464 -0.4297135 -0.5547721
## 3 -0.3469466 -0.4378458 -0.5615263
## 4 -0.3257375 -0.3733845 -0.6884601
## 5 -0.3441509 -0.3726687 -0.5139517
## 6 -0.1041294 -0.2396507 -0.3413784
## fBodyGyro.mean...Z fBodyGyro.std...X fBodyGyro.std...Y fBodyGyro.std...Z
## 1 -0.2559409 -0.5166919 -0.03350816 -0.4365622
## 2 -0.3966599 -0.6040530 -0.53304695 -0.5598566
## 3 -0.4181262 -0.6151214 -0.56888867 -0.5458964
## 4 -0.6013811 -0.5426468 -0.65465777 -0.7164585
## 5 -0.2131270 -0.5293928 -0.50268338 -0.4203671
## 6 -0.2035755 -0.5153239 -0.33200871 -0.5122092
## fBodyGyro.meanFreq...X fBodyGyro.meanFreq...Y fBodyGyro.meanFreq...Z
## 1 0.01478450 -0.06577462 0.0007733216
## 2 0.00727968 -0.04270093 0.1397521153
## 3 0.03375969 -0.03798965 -0.0445079739
## 4 -0.12715409 -0.27466564 0.1498515347
## 5 -0.04586019 -0.01918601 0.1674578191
## 6 0.09124097 0.04162675 0.3028749498
## fBodyAccMag.mean.. fBodyAccMag.std.. fBodyAccMag.meanFreq..
## 1 -0.1286235 -0.3980326 0.1906437
## 2 -0.3242894 -0.5771052 0.3932062
## 3 -0.2900315 -0.4563731 0.1134943
## 4 -0.4508046 -0.6511726 0.3820964
## 5 -0.3049925 -0.5196369 0.1498525
## 6 -0.2013866 -0.4216831 0.2000904
## fBodyBodyAccJerkMag.mean.. fBodyBodyAccJerkMag.std..
## 1 -0.05711940 -0.10349240
## 2 -0.16906435 -0.16409197
## 3 -0.18676452 -0.08985199
## 4 -0.31858781 -0.32045870
## 5 -0.26948166 -0.30568538
## 6 -0.05540142 -0.09649997
## fBodyBodyAccJerkMag.meanFreq.. fBodyBodyGyroMag.mean.. fBodyBodyGyroMag.std..
## 1 0.093822181 -0.1992526 -0.3210180
## 2 0.207500927 -0.5307048 -0.6517928
## 3 -0.117163983 -0.5697558 -0.6326433
## 4 0.111486485 -0.6092856 -0.5939372
## 5 -0.004972878 -0.4842628 -0.5897415
## 6 -0.009228846 -0.3296811 -0.5106483
## fBodyBodyGyroMag.meanFreq.. fBodyBodyGyroJerkMag.mean..
## 1 0.26884437 -0.3193086
## 2 0.30528383 -0.5832493
## 3 0.18094646 -0.6077516
## 4 0.06973012 -0.7243274
## 5 0.25062061 -0.5480536
## 6 0.29310961 -0.3665005
## fBodyBodyGyroJerkMag.std.. fBodyBodyGyroJerkMag.meanFreq..
## 1 -0.3816019 0.19066345
## 2 -0.5581046 0.12634461
## 3 -0.5490870 0.04576230
## 4 -0.7577681 0.26535537
## 5 -0.4556653 0.05273301
## 6 -0.4080789 0.10092473
Then we are finished! Hope you enjoyed the project.