Repository navigation
Expand file tree
/
Copy path00_PIT_deduping.R
More file actions
250 lines (195 loc) · 8.85 KB
/
Copy path00_PIT_deduping.R
File metadata and controls
250 lines (195 loc) · 8.85 KB
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
library(tidyverse)
library(lubridate)
## update this for each year!
PIT_date <- as.Date("2022-01-24")
################################################
################ Wheeler Import ################
################################################
## this only has to be run by the person handling
## imports subsequent processing can use the
## generated and provided csv file
{
## pick January csv export
wheeler_zip <- file.choose()
wheeler_files <- unzip(wheeler_zip, list = TRUE)
## load in all files
project_data <- read.csv(wheeler_files[14,1])
enrollment_data <- read.csv(wheeler_files[5,1])
exit_data <- read.csv(wheeler_files[7,1])
client_data <- read.csv(wheeler_files[2,1]) %>%
mutate(num_races = AmIndAKNative + Asian + BlackAfAmerican + NativeHIPacific + White,
RaceBucket = case_when(
num_races >= 2 ~ "Multiple Races",
White == 1 ~ "White/Caucasian",
NativeHIPacific == 1 ~ "Native Hawaiian/Pacific Islander",
AmIndAKNative == 1 ~ "American Indian/Indigenous",
Asian == 1 ~ "Asian/Asian American",
BlackAfAmerican == 1 ~ "Black/African American/African",
TRUE ~ ""),
## gender options are not complete for future, MissionTracker data only has binaries this year
GenderBucket = case_when(
Male == 1 ~ "Male",
Female == 1 ~ "Female",
TRUE ~ ""
),
Initials = paste0(
substr(FirstName, 1, 1),
substr(LastName, 1, 1)),
VeteranStatus = case_when(
VeteranStatus == 0 ~ "N",
VeteranStatus == 1 ~ "Y",
TRUE ~ ""
))
disability_data <- read.csv(wheeler_files[3,1]) %>%
filter(IndefiniteAndImpairs == 1) %>%
group_by(PersonalID) %>%
mutate(
DisabilityResponse = if_else(
DisabilityType == 10 & DisabilityResponse %in% c(1, 2, 3),
DisabilityResponse, NULL),
Alcohol.Abuse = max(
if_else(DisabilityResponse %in% c(1, 3), "Y", "")),
Drug.Abuse = max(
if_else(DisabilityResponse %in% c(2, 3), "Y", "")),
Physical.Disability = max(
if_else(DisabilityType == 5, "Y", "")),
Mental.Health = max(
if_else(DisabilityType == 9, "Y", "")),
HIV.AIDS = max(
if_else(DisabilityType == 8, "Y", "")),
Chronic.Health.Condition = max(
if_else(DisabilityType == 7, "Y", ""))
) %>%
select(PersonalID, Alcohol.Abuse, Drug.Abuse, Physical.Disability,
Mental.Health, HIV.AIDS, Chronic.Health.Condition) %>%
distinct()
# would pull in DV data if anyone was marked fleeing, but no one is
# dv_data <- read.csv(wheeler_files[10,1])
## join up all files
PIT_enrollments <- enrollment_data %>%
select(EnrollmentID, PersonalID, EntryDate, HouseholdID, RelationshipToHoH, ProjectID,
TimesHomelessPastThreeYears, MonthsHomelessPastThreeYears) %>%
left_join(client_data %>%
select(PersonalID, Initials, DOB, RaceBucket, GenderBucket, VeteranStatus, Ethnicity), by = "PersonalID") %>%
left_join(disability_data, by = "PersonalID") %>%
left_join(exit_data %>%
select(EnrollmentID, ExitDate), by = "EnrollmentID") %>%
left_join(project_data %>%
select(ProjectID, ProjectName), by = "ProjectID") %>%
mutate(ProjectID = if_else(ProjectID == 0, 4, as.double(ProjectID)),
DOB = mdy(DOB, quiet = TRUE),
SurveyID = "", SurveyDate = PIT_date, Interviewer = "MissionTracker", SurveyLocation = "MissionTracker",
# no DV data recorded this year, revisit in 2023
Currently.Fleeing.DV = "",
FirstTimeHomeless = if_else(TimesHomelessPastThreeYears %in% c(2, 3, 4),
"N", "Y"),
MonthsHomelessLast3Years = case_when(
MonthsHomelessPastThreeYears > 100 &
MonthsHomelessPastThreeYears < 112 ~ "0-11",
MonthsHomelessPastThreeYears >= 112 ~ "12+",
TRUE ~ ""),
Ethnicity = case_when(
Ethnicity == 1 ~ "Y",
Ethnicity == 0 ~ "N",
TRUE ~ ""
),
TimesHomelessLast3Years = if_else(
TimesHomelessPastThreeYears %in% c(1, 2, 3, 4),
as.character(TimesHomelessPastThreeYears), "")
) %>%
filter(EntryDate <= PIT_date &
(is.na(ExitDate) |
ExitDate > PIT_date) &
ProjectID != 5) %>%
arrange(desc(EntryDate)) %>%
distinct(PersonalID, .keep_all = TRUE) %>%
rename(
StayMonday = ProjectName,
ClientID = PersonalID,
Date.of.Birth = DOB) %>%
select(SurveyID, SurveyDate, Interviewer, SurveyLocation, StayMonday, ClientID,
# ChildrenWithYou, NumberofChildren,
TimesHomelessLast3Years, MonthsHomelessLast3Years,
VeteranStatus,
Currently.Fleeing.DV, Alcohol.Abuse, Drug.Abuse, Physical.Disability,
Mental.Health, HIV.AIDS, Chronic.Health.Condition,
Ethnicity,
RaceBucket, GenderBucket, Initials, Date.of.Birth, FirstTimeHomeless
)
write.csv(PIT_enrollments, file = "Wheeler_PIT.csv", row.names = FALSE)
rm(list = ls(all.names = TRUE))
}
## select and load in .csv file with sheltered client data
sheltered_data <- file.choose(); sheltered_data <- read.csv(sheltered_data)
## fix date of birth formatting issue (caused by null DOBs)
cleaned_sheltered_data <- sheltered_data %>%
mutate(Date.of.Birth = case_when(
!is.na(Date.of.Birth) ~ as.Date(
as.POSIXct(Date.of.Birth, format = "%m/%d/%Y %I:%M %p"))),
InShelterSince = as.Date(
as.POSIXct(InShelterSince, format = "%m/%d/%Y")
),
FirstTimeHomeless = case_when(
is.na(TimesHomelessLast3Years) | TimesHomelessLast3Years == 1 ~ "Y",
TRUE ~ "N"),
TimesHomelessLast3Years = as.character(TimesHomelessLast3Years),
RaceBucket = case_when(
RaceBucket == "White" ~ "White/Caucasian",
RaceBucket == "Multi-Racial" ~ "Multiple Races",
RaceBucket == "Native Hawaiian or Pacific Islander" ~ "Native Hawaiian/Pacific Islander",
RaceBucket =="American Indian, Alaska Native, or Indigenous" ~ "American Indian/Indigenous",
RaceBucket == "Asian or Asian American" ~ "Asian/Asian American",
RaceBucket == "Black, African American, or African" ~ "Black/African American/African",
TRUE ~ ""),
GenderBucket = case_when(
Gender == "Male,Data not collected" ~ "Male",
Gender == "Transgender,Male" ~ "Transgender",
Gender == "A gender other than singularly female or male (e.g., non-binary, genderfluid, agender, culturally specific gender)" ~
"A gender other than singularly female or male",
TRUE ~ Gender
))
## select and load in .csv file with unsheltered client data
unsheltered_data <- file.choose(); unsheltered_data <- read.csv(unsheltered_data, fileEncoding="UTF-8-BOM")
## trim any initials of more than two characters down to the first and last
## character only
cleaned_unsheltered_data <- unsheltered_data %>%
mutate(Initials = paste0(
substr(Initials, 1, 1),
substr(Initials, nchar(Initials), nchar(Initials))
),
Date.of.Birth = case_when(
!is.na(Date.of.Birth) ~ as.Date(
as.POSIXct(Date.of.Birth, format = "%m/%d/%Y %I:%M %p"))),
Mental.Health = case_when(
Mental.Health == "Y" | PTSD == "Y" | BrainInjury.HeadTrauma == "Y" ~ "Y",
TRUE ~ ""),
FirstTimeHomeless = if_else(TimesHomelessLast3Years %in% c("2", "3", "4", "4+"),
"N", FirstTimeHomeless),
GenderBucket = if_else(GenderBucket == "male", "Male", GenderBucket))
## get names of columns shared by both files
common_columns <- intersect(unlist(colnames(cleaned_sheltered_data)),
unlist(colnames(cleaned_unsheltered_data)))
## join HMIS shelter and unsheltered files based on those common columns and de-duplicate
cleaned_data <- cleaned_sheltered_data %>%
full_join(cleaned_unsheltered_data,
by = common_columns)
## load in Wheeler csv data
wheeler_data <- file.choose(); wheeler_data <- read.csv(wheeler_data)
wheeler_data <- wheeler_data %>%
mutate(TimesHomelessLast3Years = as.character(TimesHomelessLast3Years),
Date.of.Birth = ymd(Date.of.Birth))
## get names of columns shared by both files
common_columns <- intersect(unlist(colnames(cleaned_data)),
unlist(colnames(wheeler_data)))
## join PIT and Wheeler files based on those common columns and de-duplicate
cleaned_data <- cleaned_data %>%
full_join(wheeler_data,
by = common_columns) %>%
group_by(Initials, Date.of.Birth, GenderBucket, RaceBucket) %>%
mutate(Ethnicity = max(if_else(Ethnicity == "", "DK/R", Ethnicity)),
VeteranStatus = max(if_else(VeteranStatus == "", "DK/R", VeteranStatus)),
FirstTimeHomeless = max(if_else(FirstTimeHomeless == "", "R", FirstTimeHomeless))) %>%
arrange(desc(InShelterSince)) %>%
slice(1L) %>%
ungroup()
write.csv(cleaned_data, "cleaned_pit_data.csv", row.names = FALSE)