forked from arundeep78/get_clean_data_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_analysis_util.R
67 lines (45 loc) · 2.16 KB
/
run_analysis_util.R
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
# This file contains helper functions that helps the main script run_analysis.R
#
join_data = function(folder_path, file1, file2){
# This function reads two files of similar datasets, merge and returns them
if(!file.exists(folder_path)){ return("folder path is invalid")}
fullpath_file1 = file.path(folder_path,file1)
fullpath_file2 = file.path(folder_path,file2)
data1 = data2 = NULL
if(!file.exists(fullpath_file1) | !file.exists(fullpath_file2)){
return("please check the file names")
}
data1 = read.table(fullpath_file1 )
data2 = read.table(fullpath_file2)
# This is only a basic check on column shape. It does not check the data types
# . Even it checks, it is hard to verify if they represent same data
if(length(data1)!=length(data2)){
return("two datasets have different number of columns")
}
rbind(data1,data2)
}
get_feature_cols_means_std = function(folder_path,features_file){
# This function get the column numbers for al features that has "mean" and "std" values
if(!file.exists(folder_path)){ return("folder path is invalid")}
fullpath_feature_file = file.path(folder_path,features_file)
if(!file.exists(fullpath_feature_file)){
return("Check filename of the features file")
}
features = read.table(fullpath_feature_file)
#get column numbers and names where description has mean or std in it
col_nums =grep("mean|std",features[,2])
col_names = grep("mean|std",features[,2],value = TRUE)
# clean names for any special characters
col_names = gsub("[()-]","",col_names)
list("col_numbers"=col_nums,"col_names"=col_names)
}
get_activity_names = function(folder_path, activity_file){
# This function get the activity names as text corresponding to each number
if(!file.exists(folder_path)){ return("folder path is invalid")}
fullpath_activity_file = file.path(folder_path,activity_file)
if(!file.exists(fullpath_activity_file)){
return("Check filename of the activity file")
}
activities = read.table(fullpath_activity_file)
activities
}