-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraw-cleaning.R
433 lines (263 loc) · 13.4 KB
/
raw-cleaning.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# raw overview of HOMBRES Data
# Ana Bravo
# last update: 2023-04-29
# raw cleaning exploring data
# Ana Bravo
# last update: 2023-04-29
# set up ------------------------------------------------------------------------------
library(haven)
library(tidyverse)
# bring .sav file into R using Haven ------------------------------------------------------------------------------
HOMBRES_Y_subset <- haven::read_sav("raw_data/Hombres_Youth_Subset.sav",
encoding = "latin1")
# Hombres .sav file of US born youth. Only includes baseline information ---------------------------------------------
HOMBRES_Youth_USborn_Baseline <- haven::read_sav("raw_data/Hombres Youth USborn Baseline.sav",
encoding = "latin1")
# writing R file into .csv file ------------------------------------------------------------------------------
write_csv(x = HOMBRES_Y_subset,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/raw_data/HOMBRES_Y_Raw.csv")
# bringing in HOMBRES raw CSV ------------------------------------------------------------------------------
HOMBRES_Y_Raw <- read_csv("raw_data/HOMBRES_Y_Raw.csv")
## cleaning data - names ------------------------------------------------------------------------------
copy_HOMBRES_Y_subset <- HOMBRES_Y_Raw # making copy of data
names(copy_HOMBRES_Y_subset) <- NULL # removing previous names
newnames <- c("Redcap_event_name", # creating new names
"Participant_ID",
"Father_or_son",
"Language_pref",
"Total_household_n",
"Age_at_baseline",
"Currently_in_school",
"Current_grade",
"Time_point",
"MACV_RSP",
"MACV_REL",
"Machismo_SS",
"Caballerismo_SS",
"HSI_Y",
"FES_COFLT",
"FES_RELIG",
"FES_CONTROL",
"BAS_Americanism",
"BAS_Hispanicism",
"SemiRural_or_urban",
"Group",
"Parent_marital_status",
"Pastmonth_total_income")
names(copy_HOMBRES_Y_subset) <-newnames # pasting new names on copy HOMBRES data
### removing Y variable ###
clean_names_HOMBRES_Y <- clean_names_HOMBRES_Y %>%
mutate(
Participant_ID = str_remove_all(Participant_ID, "[A-Z]")
)
## converting participant ID to numebric ####
clean_names_HOMBRES_Y$Participant_ID = as.numeric(clean_names_HOMBRES_Y$Participant_ID)
clean_HOMBRES_Y_subset <- clean_names_HOMBRES_Y
## dropig redcap event - already have my baseline and 6mfu in time_point variable ######
clean_HOMBRES_Y_subset <- clean_HOMBRES_Y_subset %>%
select(Redcap_event_name, Participant_ID, Time_point, Group, everything())
## creating intervention/control numerical variable ####
HOMBRES_Y_clean <- clean_HOMBRES_Y_subset %>%
mutate(Group_numerical = ifelse(Group == "C", 0, 1)) %>%
select(Redcap_event_name ,Participant_ID, Time_point, Group, Group_numerical, everything())
### pivot to wide data set ------------------------------------------------------------------------------
HOMBRES_clean_wide <- HOMBRES_Y_clean %>%
select(-Time_point) %>%
pivot_wider(
names_from = Redcap_event_name,
values_from = c(Group, Group_numerical, Father_or_son, Language_pref, Total_household_n,
Age_at_baseline, Currently_in_school, Current_grade, MACV_RSP, MACV_REL,
Machismo_SS, Caballerismo_SS, HSI_Y, FES_COFLT, FES_RELIG, FES_CONTROL,
BAS_Americanism, BAS_Hispanicism, SemiRural_or_urban, Parent_marital_status,
Pastmonth_total_income)
)
# writing clean data set - wide format ------------------------------------------------------------------------------
write_csv(
x = HOMBRES_clean_wide,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_Y_clean_wide.csv"
)
# writing clean data set - long format
write_csv(x = HOMBRES_Y_clean,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_Y_clean.csv")
# writing copy HOMBRES ------------------------------------------------------------------------------
write_csv(x = copy_HOMBRES_Y_subset,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_Y_clean_copy.csv")
some table prep ------------------------------------------------------------------------------
# table prep
table_1 <-
list(
"tbl_summary-str:default_con_type" = "continuous2",
"tbl_summary-str:continuous_stat" = c(
"{median} ({p25} - {p75})",
"{mean} ({sd})",
"{min} - {max}"
),
"tbl_summary-str:categorical_stat" = " ({p}%)",
"style_number-arg:big.mark" = "",
"tbl_summary-fn:percent_fun" = function(x) style_percent(x, digits = 2)
)
# subset for table -------------------------------------------------------------------------
table_subset <- subset_YRBS_2019 %>%
select(Sex,
SexOrientation,
Q4,
Q30,
Q34,
Q41,
Q45
)
#actual table ------------------------------------------------------------------------------
gtsummary::set_gtsummary_theme(table_1)
table_subset %>%
gtsummary::tbl_summary(
by = SexOrientation,
missing = "always",
missing_text = "Missing",
list(
Sex ~ "Sex", Q4 ~ "Hispanic", SexOrientation ~"Sexual Orientation",
Q30 ~ "Cig smoke", Q34 ~"Vape", Q41 ~ "Alcohol", Q45 ~ "Marijuana"
)
) %>%
bold_labels() %>%
modify_header(label ~ "**Variables**")
write_csv(x = copy_clean_HOMBRES_Y,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/copy_CLEAN_HOMBRES_Y.csv")
###############################################################################
##################### Jan 26 2023 #############################################
###############################################################################
# prep to export SAS csv file to merge with US born data set ------------------
new_copy_hombres_youth <- read_csv("raw_data/new_copy_hombres_youth_toR.csv")
# unique(HOMBRES_Youth_USborn_Baseline[,"acaseid"])
# unique(HOMBRES_Y_subset[,"acaseid"])
# writing US born into csv file ------------------------------------------------------------------------------
write_csv(x = HOMBRES_Youth_USborn_Baseline,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/raw_data/HOMBRES_Youth_USborn_Baseline.csv")
### bring in US born ------------------------------------------------------------------------------
HOMBRES_Youth_USborn_Baseline <- read_csv("raw_data/HOMBRES_Youth_USborn_Baseline.csv")
### fixing variable name to match SAS data ----------------------------------------------------
names(HOMBRES_Youth_USborn_Baseline) <- NULL
new_USborn_names <- c(
"Participant_ID",
"US_born"
)
# attempt to join data sets (first attempt) --------------------------------------------------------
HOMBRES_joined_USborn <- left_join(
new_copy_hombres_youth,
HOMBRES_Youth_USborn_Baseline,
by = "Participant_ID"
)
###### making the follow up NA, cause this is baseline only data ##############
## not sure why case_when() function is misbehaving here/not working###########
HOMBRES_joined_USborn_forSAS <-
HOMBRES_joined_USborn %>%
mutate(US_born= ifelse(
Redcap_event_name == "second_post_interv_arm_1",
NA,
US_born
))
## organizing variables for final transfer to SAS -----------------------------------------
HOMBRES_joined_USborn_forSAS <- HOMBRES_joined_USborn_forSAS %>%
select(Redcap_event_name, Participant_ID, Father_or_son, Language_pref,
Currently_in_school, US_born, Time_point,everything())
# write SAS/ or CSV ----------------------------------------------------------------------------
write_sas(HOMBRES_joined_USborn_forSAS,
path = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_joined_USborn_forSAS.sas7bdat")
write_csv(
HOMBRES_joined_USborn_forSAS,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_joined_USborn.csv"
)
# changing martial status to numerical values if needed ------------------------------------------
HOMBRES_joined_USborn <- read_csv("clean_data/HOMBRES_joined_USborn.csv")
# checking values of variable parent martital status --------------------------------------------------
unique(HOMBRES_joined_USborn$Parent_marital_status)
HOMBRES_joined_USborn %>%
mutate(Parent_marital_status = case_when(
Parent_marital_status == "Married" ~ 1,
Parent_marital_status == "In a domestic relationship" ~ 2,
Parent_marital_status == "Single or separated" ~ 3
))
###############################################################################
######## Updated and cleaning on new data based on feedback given on:##########
################## ############2/2/2023 #######################################
HOMBRES_joined_USborn <- read_csv("clean_data/HOMBRES_joined_USborn.csv")
HOMBRES_Father_prepost <- haven::read_sav("raw_data/Hombres Father selected vars pre and post.sav",
encoding = "latin1")
# transferring fixed variables to post time point for correct analysis ------------------------------------
HOMBRES_Youth_fixed <- HOMBRES_joined_USborn %>%
group_by(Participant_ID) %>%
fill(Language_pref, .direction = "updown") %>%
fill(Currently_in_school, .direction = "updown") %>%
fill(US_born, .direction = "updown") %>%
fill(numeric_age, .direction = "updown") %>%
fill(numeric_current_grade, .direction = "updown") %>%
fill(numeric_total_household, .direction = "updown")
# changing variable Time_point to Time in youth data ------------------------------------------
HOMBRES_Youth_fixed <- HOMBRES_Youth_fixed %>%
rename(Time = Time_point) %>%
select(Redcap_event_name, Participant_ID, Time, everything())
#export data ----------------------------------------------------------------------------------
write_csv(
x = HOMBRES_Youth_fixed,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/HOMBRES_Youth_fixed.csv"
)
# Fixing variables in Father data set ---------------------------------------------------------------------
names(HOMBRES_Father_prepost) <- NULL
fathers_newnames <- c(
"Participant_ID",
"Years_in_US_notborn_F",
"Current_Age_F",
"Born_in_US_F",
"Time",
"Drink_Freq_F",
"Drink_Quant_F",
"Binge_Drinker_F"
)
names(HOMBRES_Father_prepost) <- fathers_newnames
write_csv(x = HOMBRES_Father_prepost,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/raw_data/HOMBRES_Father_Raw.csv")
HOMBRES_Father_cleanNames <- read_csv("raw_data/HOMBRES_Father_Raw.csv")
################################################################################
# preparing to merge with Youth data set by participant ID #####################
########### cleaning Father Participant ID variables ###########################
# switching From F variable to Y variable ------------------------------------------------------------
HOMBRES_Father_cleanNames <- HOMBRES_Father_cleanNames %>%
mutate(Participant_ID = str_replace_all(
Participant_ID,
"[A-Z]",
"Y"
))
# preparing to join Father dad with youth data set --------------------------------------------------
### maybe subsetting the data first for youth###
HOMBRES_Youth_subset <- HOMBRES_Youth_fixed %>%
select(Participant_ID, Time, HSI_Y)
### trying to merge youth subset to father data#####
Youth_Father_subset <- left_join(
HOMBRES_Father_cleanNames,
HOMBRES_Youth_subset,
by = c("Participant_ID", "Time")
) %>%
select(Participant_ID, Time, HSI_Y, everything())
###trying to merge some father significant variables with Youth subset #####
Youth_Father_second_subset <- left_join(
HOMBRES_Father_cleanNames,
HOMBRES_Youth_fixed,
by = c("Participant_ID", "Time")
) %>%
select(Redcap_event_name, Participant_ID, Time, HSI_Y, everything())
# exporting new data set with father years in the US -----------------------------------------------
write_csv(x = Youth_Father_second_subset,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/Father_Youth_second_subset.csv")
# exporting the new subset data of youth and father info ---------------------------------------------
write_csv(x = Youth_Father_subset,
file = "/Users/anbravo/OneDrive/FIU Related/Graduate/MPH Biostatistics/CRUSADA/clean_data/Youth_Father_stress_subset.csv")
# pivot wide youth to merge? ------------------------------------------------------------------------------
#HOMBRES_Youth_wide <- HOMBRES_Youth_fixed %>%
# select(-Time_point) %>%
#pivot_wider(
#names_from = Redcap_event_name,
# values_from = c(Father_or_son, Language_pref, Currently_in_school,
#US_born, MACV_RSP, MACV_REL, Caballerismo_SS, HSI_Y, FES_COFLT,
#FES_RELIG, FES_CONTROL, BAS_Americanism, BAS_Hispanicism, SemiRural_or_urban, Group,
#Parent_marital_status, Pastmonth_total_income, numeric_age, numeric_total_household,
#numeric_current_grade, Machismo_SS)
# )