/
house.R
242 lines (201 loc) · 7.08 KB
/
house.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
#' Do Any Addresses Have House Numbers
#'
#' @description Determine whether the house number test returns any matches.
#'
#' @usage pm_house_any(.data)
#'
#' @param .data A postmastr object created with \link{pm_prep}
#'
#' @return A logical scalar is returned that is \code{TRUE} if the data contains at least
#' one house number and \code{FALSE} if they do not.
#'
#' @export
pm_house_any <- function(.data){
# check for object and key variables
if (pm_has_uid(.data) == FALSE){
stop("The variable 'pm.uid' is missing from the given object. Create a postmastr object with pm_identify and pm_prep before proceeding.")
}
if (pm_has_address(.data) == FALSE){
stop("The variable 'pm.address' is missing from the given object. Create a postmastr object with pm_prep before proceeding.")
}
# test and create output
.data <- pm_house_detect(.data)
out <- any(.data$pm.hasHouse)
# return output
return(out)
}
#' Do All Addresses Have House Numbers
#'
#' @description Determine whether the house number test returns matches for every
#' observation.
#'
#' @usage pm_house_all(.data)
#'
#' @param .data A postmastr object created with \link{pm_prep}
#'
#' @return A logical scalar is returned that is \code{TRUE} if all observations contain
#' house numbers and \code{FALSE} otherwise.
#'
#' @export
pm_house_all <- function(.data){
# check for object and key variables
if (pm_has_uid(.data) == FALSE){
stop("The variable 'pm.uid' is missing from the given object. Create a postmastr object with pm_identify and pm_prep before proceeding.")
}
if (pm_has_address(.data) == FALSE){
stop("The variable 'pm.address' is missing from the given object. Create a postmastr object with pm_prep before proceeding.")
}
# test and create output
.data <- pm_house_detect(.data)
out <- all(.data$pm.hasHouse)
# return output
return(out)
}
#' Detect Presence of House Numbers
#'
#' @description Determine the presence of house numbersin a string.
#'
#' @usage pm_house_detect(.data)
#'
#' @param .data A postmastr object created with \link{pm_prep}
#'
#' @return A tibble with a new logical variable \code{pm.hasHouse} that is
#' \code{TRUE} if a house number is found in the first word of the address
#' and \code{FALSE} otherwise.
#'
#' @importFrom dplyr mutate
#' @importFrom stringr str_detect
#' @importFrom stringr word
#'
#' @export
pm_house_detect <- function(.data){
# global bindings
pm.address = NULL
# check for object and key variables
if (pm_has_uid(.data) == FALSE){
stop("The variable 'pm.uid' is missing from the given object. Create a postmastr object with pm_identify and pm_prep before proceeding.")
}
if (pm_has_address(.data) == FALSE){
stop("The variable 'pm.address' is missing from the given object. Create a postmastr object with pm_prep before proceeding.")
}
# detect pattern
.data <- dplyr::mutate(.data, pm.hasHouse = stringr::str_detect(stringr::word(pm.address, 1), pattern = "[0-9]"))
# return output
return(.data)
}
pm_house_detect_prep <- function(.data, var){
# save parameters to list
paramList <- as.list(match.call())
# unquote
if (!is.character(paramList$var)) {
varQ <- rlang::enquo(var)
} else if (is.character(paramList$var)) {
varQ <- rlang::quo(!! rlang::sym(var))
}
# detect pattern
.data <- dplyr::mutate(.data, pm.hasHouse = stringr::str_detect(stringr::word(!!varQ, 1), pattern = "[0-9]"))
# return output
return(.data)
}
#' Return Only Unmatched Observations From pm_house_detect
#'
#' @description Automatically subset the results of \link{pm_house_detect} to
#' return only observations that were not found to include a house number
#'
#' @usage pm_house_none(.data)
#'
#' @param .data A postmastr object created with \link{pm_prep}
#'
#' @return A tibble containing only observations that were not found matched
#' using the house number test. The variable created by \link{pm_house_detect},
#' \code{pm.hasHouse}, is removed.
#'
#' @importFrom dplyr %>%
#' @importFrom dplyr filter
#' @importFrom dplyr select
#'
#' @export
pm_house_none <- function(.data){
# global bindings
pm.hasHouse = NULL
# check for object and key variables
if (pm_has_uid(.data) == FALSE){
stop("The variable 'pm.uid' is missing from the given object. Create a postmastr object with pm_identify and pm_prep before proceeding.")
}
if (pm_has_address(.data) == FALSE){
stop("The variable 'pm.address' is missing from the given object. Create a postmastr object with pm_prep before proceeding.")
}
# create output
.data %>%
pm_house_detect() %>%
dplyr::filter(pm.hasHouse == FALSE) %>%
dplyr::select(-pm.hasHouse) -> out
# return output
return(out)
}
#' Parse House Numbers
#'
#' @description Parse house number data out from \code{pm.address}.
#'
#' @usage pm_house_parse(.data, locale = "us")
#'
#' @param .data A postmastr object created with \link{pm_prep}
#' @param locale A string indicating the country these data represent; the only
#' current option is "us" but this is included to facilitate future expansion.
#'
#' @return A tibble with a new column \code{pm.house} that contains the house number.
#' If a house number is not detected in the string, a value of \code{NA} will be
#' returned.
#'
#' @importFrom dplyr %>%
#' @importFrom dplyr bind_rows
#' @importFrom dplyr everything
#' @importFrom dplyr filter
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @importFrom purrr map
#' @importFrom stringr str_c
#' @importFrom stringr str_detect
#' @importFrom stringr str_length
#' @importFrom stringr str_split
#' @importFrom stringr str_sub
#' @importFrom stringr str_replace
#' @importFrom stringr word
#'
#' @export
pm_house_parse <- function(.data, locale = "us"){
# global bindings
pm.uid = pm.address = pm.house = pm.houseRange = pm.houseLow = pm.houseHigh = pm.hasHouse = NULL
# check for object and key variables
if (pm_has_uid(.data) == FALSE){
stop("The variable 'pm.uid' is missing from the given object. Create a postmastr object with pm_identify and pm_prep before proceeding.")
}
if (pm_has_address(.data) == FALSE){
stop("The variable 'pm.address' is missing from the given object. Create a postmastr object with pm_prep before proceeding.")
}
# detect individual addresses
if ("pm.hasHouse" %in% names(.data) == FALSE){
houseDetect <- FALSE
.data <- pm_house_detect(.data)
} else if ("pm.hasHouse" %in% names(.data) == TRUE){
houseDetect <- TRUE
}
# parse
.data %>%
dplyr::mutate(pm.house = ifelse(pm.hasHouse == TRUE, stringr::word(pm.address, 1), NA)) %>%
dplyr::mutate(pm.address = ifelse(pm.hasHouse == TRUE,
stringr::word(pm.address, start = 2, end = -1),
pm.address)) %>%
dplyr::select(pm.uid, pm.address, pm.house, dplyr::everything()) -> .data
# remove pm.hasHouse if not present initially
if (houseDetect == FALSE){
.data <- dplyr::select(.data, -pm.hasHouse)
}
# reorder variables
if (locale == "us"){
vars <- pm_reorder(.data)
.data <- dplyr::select(.data, vars)
}
# return output
return(.data)
}