forked from vynguyen92/publish_nhanes_data_1988_2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf - upload_nhanes_master_files.R
217 lines (183 loc) · 9.99 KB
/
f - upload_nhanes_master_files.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
upload_nhanes_master_files <- function(name_of_file)
{
library(readxl)
library(tidyverse)
# Determine the name of all sheets in this master file
sheets_name <- excel_sheets(name_of_file)[-1]
# print(sheets_name)
# sheets_name <- sheets_name[1]
# Initialize an empty list to store the dataset containing the cleaning documentation
list_of_master_files <- list()
# Determine the number of documentation datasets
num_sheets <- length(sheets_name)
for(i in seq(num_sheets))
{
# Extract the name of a given sheet
sheet_name_i <- sheets_name[i]
print(sheet_name_i)
if(sheet_name_i == "Response")
{
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i
, col_types = c("text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "text"
, "numeric"
, "text"
, "text"
, "numeric"
, "text"
, "text"
, "numeric"
, "text"
, "text"))
} else if(sheet_name_i == "Dietary") {
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i
, col_types = c("text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "text"
, "numeric"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "text"
, "text"
, "numeric"
, "text"
, "numeric"
, "text"))
} else if(sheet_name_i == "Chemicals") {
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i
, col_types = c("text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "numeric"
, "numeric"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "numeric"
, "text"
, "numeric"
, "text"
, "text"
, "text"
, "text"
, "numeric" ))
} else if(sheet_name_i == "Weights") {
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i
, col_types = c("text"
, "text"
, "text"
, "text"
, "numeric"
, "text"
, "text"
, "text"
, "text"
, "text"))
} else if(sheet_name_i == "Questionnaire") {
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i
, col_types = c("text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "text"
, "numeric"
, "text"
, "numeric"
, "text"
, "text"
, "text"
, "numeric"
, "text"))
} else {
# Read in a documentation dataset using the name of the sheet
excel_sheet_i <- read_excel(name_of_file
, sheet = sheet_name_i)
}
# View(excel_sheet_i)
# If the documentation dataset pertains to the chemical, then include two extra columns to indicate the
# chemical and comment codenames to use after harmonizing to have only one codename for one chemical or comment
if(sheet_name_i == "Chemicals")
{
# Define a vector to contain the column name pertaining original codenames
original_codenames <- c("chemical_codename"
, "comment_codename")
# Define a vector to contain the new column names
new_correcting_codenames <- c("chemical_codename_use"
, "comments_codename_use")
# # Add a new column to contain the harmonized codenames
# excel_sheet_i <- include_column_for_harmonized_codenames(original_codenames
# , new_correcting_codenames
# , excel_sheet_i)
# # Ensure that the chemical codename and chemical codename to use are at the beginning
# excel_sheet_i <- excel_sheet_i %>%
# relocate(chemical_codename_use
# , .after = chemical_codename)
# View(excel_sheet_i %>%
# select(chemical_codename_use
# , comment_codename
# , corrected_comment_codename
# , comments_codename_use) %>%
# unique(.))
} else if(grepl("Fix Category", sheet_name_i) == FALSE
& !(sheet_name_i %in% c("Weights", "Mortality", "Biomonitoring Equivalents"))) {
# Define a vector to contain the column name pertaining original codenames
original_codenames <- "variable_codename"
# Define a vector to contain the new column names
new_correcting_codenames <- c("variable_codename_use")
# # Add a new column to contain the harmonized codenames
# excel_sheet_i <- include_column_for_harmonized_codenames(original_codenames
# , new_correcting_codenames
# , excel_sheet_i)
# # Ensure that the chemical codename and chemical codename to use are at the beginning
# excel_sheet_i <- excel_sheet_i %>%
# relocate(variable_codename_use
# , .after = variable_codename)
} else {
}
# Store each documentation dataset into the list
list_of_master_files[[sheet_name_i]] <- excel_sheet_i
}
return(list_of_master_files)
}