@@ -89,6 +89,10 @@ zi_aggregate <- function(.data, year, extensive = NULL, intensive = NULL,
8989 output = " tidy" , zcta = NULL , key = NULL ){
9090
9191 # evaluate inputs
92+ # if (is.null(.data)){
93+ # stop("The '.data' object provided is NULL. Please provide a dataframe.")
94+ # }
95+
9296 if (missing(year )){
9397 stop(" The 'year' value is missing. Please provide a numeric value between 2010 and 2022." )
9498 }
@@ -192,7 +196,11 @@ zi_aggregate <- function(.data, year, extensive = NULL, intensive = NULL,
192196 weights <- zi_census_weights(year = year , key = key )
193197
194198 # # aggregate
195- out <- zi_census_intensive(.data , weights = weights , method = intensive_method )
199+ if (! is.null(weights )){
200+ out <- zi_census_intensive(.data , weights = weights , method = intensive_method )
201+ } else {
202+ out <- NULL
203+ }
196204
197205 } else if (extensive_id == TRUE & intensive_id == TRUE ){
198206
@@ -203,13 +211,17 @@ zi_aggregate <- function(.data, year, extensive = NULL, intensive = NULL,
203211 # # calculate weights
204212 weights <- zi_census_weights(year = year , key = key )
205213
206- # # aggregate
207- extensive_df <- zi_census_extensive(extensive_df )
208- intensive_df <- zi_census_intensive(intensive_df , weights = weights , method = intensive_method )
214+ if (! is.null(weights )){
215+ # # aggregate
216+ extensive_df <- zi_census_extensive(extensive_df )
217+ intensive_df <- zi_census_intensive(intensive_df , weights = weights , method = intensive_method )
209218
210- # # combine
211- out <- dplyr :: bind_rows(extensive_df , intensive_df )
212- out <- dplyr :: arrange(out , ZCTA3 , variable )
219+ # # combine
220+ out <- dplyr :: bind_rows(extensive_df , intensive_df )
221+ out <- dplyr :: arrange(out , ZCTA3 , variable )
222+ } else {
223+ out <- NULL
224+ }
213225
214226 }
215227
@@ -227,7 +239,11 @@ zi_aggregate <- function(.data, year, extensive = NULL, intensive = NULL,
227239 weights <- zi_acs_weights(year = year , survey = survey , key = key )
228240
229241 # # aggregate
230- out <- zi_acs_intensive(.data , weights = weights , method = intensive_method )
242+ if (! is.null(weights )){
243+ out <- zi_acs_intensive(.data , weights = weights , method = intensive_method )
244+ } else {
245+ out <- NULL
246+ }
231247
232248 } else if (extensive_id == TRUE & intensive_id == TRUE ){
233249
@@ -238,43 +254,47 @@ zi_aggregate <- function(.data, year, extensive = NULL, intensive = NULL,
238254 # # calculate weights
239255 weights <- zi_acs_weights(year = year , survey = survey , key = key )
240256
241- # # aggregate
242- extensive_df <- zi_acs_extensive(extensive_df )
243- intensive_df <- zi_acs_intensive(intensive_df , weights = weights , method = intensive_method )
244-
245- # # combine
246- out <- dplyr :: bind_rows(extensive_df , intensive_df )
247- out <- dplyr :: arrange(out , ZCTA3 , variable )
248-
257+ if (! is.null(weights )){
258+ # # aggregate
259+ extensive_df <- zi_acs_extensive(extensive_df )
260+ intensive_df <- zi_acs_intensive(intensive_df , weights = weights , method = intensive_method )
261+
262+ # # combine
263+ out <- dplyr :: bind_rows(extensive_df , intensive_df )
264+ out <- dplyr :: arrange(out , ZCTA3 , variable )
265+ } else {
266+ out <- NULL
267+ }
249268 }
250269
251270 }
252271
253- # optionally subset
254-
255- if (is.null(zcta ) == FALSE ){
256- out <- dplyr :: filter(out , ZCTA3 %in% zcta == TRUE )
257- }
272+ if ( ! is.null( out )){
273+ # optionally subset
274+ if (is.null(zcta ) == FALSE ){
275+ out <- dplyr :: filter(out , ZCTA3 %in% zcta == TRUE )
276+ }
258277
259- # optionally pivot
260- if (output == " wide" ){
278+ # optionally pivot
279+ if (output == " wide" ){
261280
262- # # prep names
263- out <- dplyr :: rename(out , " E" = " estimate" , " M" = " moe" )
281+ # # prep names
282+ out <- dplyr :: rename(out , " E" = " estimate" , " M" = " moe" )
264283
265- # # pivot
266- out <- tidyr :: pivot_wider(out , id_cols = " ZCTA3" , names_from = " variable" ,
267- names_glue = " {variable}{.value}" ,
268- values_from = c(" E" , " M" ))
284+ # # pivot
285+ out <- tidyr :: pivot_wider(out , id_cols = " ZCTA3" , names_from = " variable" ,
286+ names_glue = " {variable}{.value}" ,
287+ values_from = c(" E" , " M" ))
269288
270- # # re-order names alphabetically
271- wide_names <- names(out )
272- wide_names <- wide_names [wide_names != " ZCTA3" ]
273- wide_names <- c(" ZCTA3" , sort(wide_names ))
289+ # # re-order names alphabetically
290+ wide_names <- names(out )
291+ wide_names <- wide_names [wide_names != " ZCTA3" ]
292+ wide_names <- c(" ZCTA3" , sort(wide_names ))
274293
275- # # re-order columns alphabetically
276- out <- dplyr :: select(out , wide_names )
294+ # # re-order columns alphabetically
295+ out <- dplyr :: select(out , wide_names )
277296
297+ }
278298 }
279299
280300 # return output
@@ -332,32 +352,29 @@ zi_census_weights <- function(year, key){
332352 GEOID = NAME = ZCTA3 = total_pop = value = weight = NULL
333353
334354 # # call get_decennial
335- out <- suppressMessages(
336- tidycensus :: get_decennial(
337- geography = " zcta" ,
338- variables = " P001001" ,
339- year = year ,
340- output = " tidy" ,
341- key = key
342- ))
343-
344- # # prep data
345- out <- dplyr :: mutate(out , ZCTA3 = substr(GEOID , 1 , 3 ), .before = GEOID )
346- out <- dplyr :: select(out , - NAME )
347- out <- dplyr :: arrange(out , ZCTA3 )
355+ out <- zi_get_decennial(geography = " zcta" , variables = " P001001" ,
356+ table = NULL , year = year , output = " tidy" ,
357+ survey = NULL , key = key )
348358
349- # # group by and sum
350- totals <- dplyr :: group_by(out , ZCTA3 )
351- totals <- dplyr :: summarise(totals , total_pop = sum(value , na.rm = TRUE ))
359+ if (! is.null(out )){
360+ # # prep data
361+ out <- dplyr :: mutate(out , ZCTA3 = substr(GEOID , 1 , 3 ), .before = GEOID )
362+ out <- dplyr :: select(out , - NAME )
363+ out <- dplyr :: arrange(out , ZCTA3 )
352364
353- # # join
354- out <- dplyr :: left_join(out , totals , by = " ZCTA3" )
365+ # # group by and sum
366+ totals <- dplyr :: group_by(out , ZCTA3 )
367+ totals <- dplyr :: summarise(totals , total_pop = sum(value , na.rm = TRUE ))
355368
356- # # calculate proportions
357- out <- dplyr :: mutate (out , weight = value / total_pop )
369+ # # join
370+ out <- dplyr :: left_join (out , totals , by = " ZCTA3 " )
358371
359- # # subset
360- out <- dplyr :: select(out , ZCTA3 , weight )
372+ # # calculate proportions
373+ out <- dplyr :: mutate(out , weight = value / total_pop )
374+
375+ # # subset
376+ out <- dplyr :: select(out , ZCTA3 , weight )
377+ }
361378
362379 # # return output
363380 return (out )
@@ -424,33 +441,30 @@ zi_acs_weights <- function(year, survey, key){
424441 GEOID = NAME = ZCTA3 = total_pop = estimate = weight = NULL
425442
426443 # # call get_acs
427- out <- suppressMessages(
428- tidycensus :: get_acs(
429- geography = " zcta" ,
430- variables = " B01003_001" ,
431- year = year , output = " tidy" ,
432- survey = survey ,
433- key = key
434- ))
435-
436- # # prep data
437- out <- dplyr :: mutate(out , GEOID = stringr :: word(NAME , 2 ))
438- out <- dplyr :: mutate(out , ZCTA3 = substr(GEOID , 1 , 3 ), .before = GEOID )
439- out <- dplyr :: select(out , - NAME )
440- out <- dplyr :: arrange(out , ZCTA3 )
444+ out <- zi_get_acs(geography = " zcta" , variables = " B01003_001" ,
445+ table = NULL , year = year , output = " tidy" ,
446+ survey = survey , key = key )
441447
442- # # group by and sum
443- totals <- dplyr :: group_by(out , ZCTA3 )
444- totals <- dplyr :: summarise(totals , total_pop = sum(estimate , na.rm = TRUE ))
448+ if (! is.null(out )){
449+ # # prep data
450+ out <- dplyr :: mutate(out , GEOID = stringr :: word(NAME , 2 ))
451+ out <- dplyr :: mutate(out , ZCTA3 = substr(GEOID , 1 , 3 ), .before = GEOID )
452+ out <- dplyr :: select(out , - NAME )
453+ out <- dplyr :: arrange(out , ZCTA3 )
445454
446- # # join
447- out <- dplyr :: left_join(out , totals , by = " ZCTA3" )
455+ # # group by and sum
456+ totals <- dplyr :: group_by(out , ZCTA3 )
457+ totals <- dplyr :: summarise(totals , total_pop = sum(estimate , na.rm = TRUE ))
448458
449- # # calculate proportions
450- out <- dplyr :: mutate (out , weight = estimate / total_pop )
459+ # # join
460+ out <- dplyr :: left_join (out , totals , by = " ZCTA3 " )
451461
452- # # subset
453- out <- dplyr :: select(out , ZCTA3 , weight )
462+ # # calculate proportions
463+ out <- dplyr :: mutate(out , weight = estimate / total_pop )
464+
465+ # # subset
466+ out <- dplyr :: select(out , ZCTA3 , weight )
467+ }
454468
455469 # # return output
456470 return (out )
0 commit comments