How to use R’s GetZips function in the ZipRadius package to find zipcodes near a location

What The getZips Function Does

getZips yields the file of us zipcodes as a character bearing which have zip codes that fall in a designated radius in lower case pattern for utilization in choroplethrZip

Syntax – getZips ()

getZips(zipcode, radius)

Disputes

zipcode
the mentioned zip code of which you’d like the file of zip codes inside a give radius as type

radius
the measurement in miles from the middle of the given zip to the middle of the other zips as arithmetic

NOT RUN {
getZips(“30316”, 10)

# }

If you provide it a US zip code and a radius in miles it yields a data grid containing all of the US zip codes that have a middle point inside the distance detailed by the radius.

For instance these are the 8 zipcodes inside 3.5 miles of zip code 30316:

test : zipRadius(“30316”, 3.5)
test
#> zip | city | state | latitude | longitude | Distance
#> 1 30303 Atlanta GA 33.75286 -84.39013 3.26
#> 2 30307 Atlanta GA 33.76821 -84.33786 2.68
#> 3 30312 Atlanta GA 33.74574 -84.37640 2.33
#> 4 30315 Atlanta GA 33.70960 -84.38365 2.82
#> 5 30316 Atlanta GA 33.72951 -84.34087 0.00
#> 6 30317 Atlanta GA 33.75001 -84.31854 1.91
#> 7 30335 Atlanta GA 33.75241 -84.38968 3.22
#> 8 31120 Atlanta GA 33.74000 -84.38000 2.36

It also includes 3 actions which can be utilized with Ari Lamstein’s choroplethrZip package. choroplethrZip is too large to be backed by CRAN so if you desire to utilize it you must install it with:

library(devtools)
install_github(‘arilamstein/choroplethrZip@v1.3.0’)
The get states action gives a character bearing of the states in a zip radius. Here are the states inside 200 miles of 30316:

getStates(“30316”, 200)
#> [1] “alabama” “florida” “georgia” “north carolina”
#> [5] “south carolina” “tennessee”
Sometimes you’ll require a character bearing of the zip codes rather than a data grid:

getZips(“30316”, 3.5)
#> [1] “30303” “30307” “30312” “30315” “30316” “30317” “30335” “31120”

You also could desire a data grid that includes only the zip codes as the population and region for those zip codes. This utilizes an innerjoin with the population information frame from choroplethrZip so any zip codes that don’t show in both our zip list utilized for area measurements and their zip list with populations are placed. You’ll observe this now yields 6 zip codes vs. the 8 yielded with the zipRadius action.

getZipPop(“30316”, 3.5)
#> # A tibble: 6 x 2
#> location amount
#> (chr) (dbl)
#> 1 30303 4255
#> 2 30307 17344
#> 3 30312 18060
#> 4 30315 33761
#> 5 30316 31282
#> 6 30317 11520

Lastly, here’s a case of creating a choroplethr of the zip codes in the radius. I haven’t involved this action in the package because I can’t import ‘choroplethrZip’ into the package because of its capacity.

library(ZipRadius)
library(choroplethrZip)
library(ggplot2)

makeZipMap : action(zip code, radius){
choro = choroplethrZip::ZipChoropleth$new(getZipPop(zip code, radius))
suppressMessages(suppressWarnings(choro$arrange_map()))
choro$legend = “Society”
ec_zips = getZips(zip code, radius)
ec_df = choro$choropleth.df[choro$choropleth.df$region %in% ec_zips,]
ec_plot = choro$render_assister(ec_df, “”, choro$theme_wash())

ec_plot + ggplot2::coord_map()
}
makeZipMap(“30316”, 500)

———————————

#’ The zip Radius Action
#’
#’ Locate all zip codes inside a radius of a stated zip code.
#’ @param zip code The mentioned zip code of which you’d want the file of zip
#’ codes inside a give radius as a character bearing of length one.
#’ @param radius The area in miles from the middle of the provided zip to the
#’ middle of the alternative zips as arithmetic.
#’ @return A data grid with six list.
#’ the range of the code{zipcodes} dispute.
#’ describe{
#’ item{zip}{Character. A zip code close to the mentioned zip code.}
#’ item{city}{Character. The cities including the zip codes.}
#’ item{state}{Character. The states including the zip codes.}
#’ item{latitude}{Numeric. The latitudes of the middles of the zip codes.}
#’ item{longitude}{Numeric. The longitudes of the middles of the zip codes.}
#’ item{Distance}{Numeric. The range in miles from the middle of the
#’ mentioned zip code to the middle of the zip code in that tier.}
#’ }
#’ @cases
#’ # A genuine zip code
#’ zip Radius(“30316”, 10)
#’
#’ # A fake zip code
#’ zipRadius(“99999”, 10)
#’ @importFrom magrittr %>%
#’ @importFrom geosphere distHaversine
#’ @importFrom dplyr filter choice mutate
#’ @importFrom rlang .data
#’ @importFrom assertive.types assert_is_a_string assert_is_a_number
#’@export

zipRadius : action(zip code, radius) {
assertive.types::assert_is_a_string(zip code)
assertive.types::assert_is_a_number(radius)

# Get the lat/lon of the mentioned zip
refPoint : dplyr::filter(zipList, .data$zip == zipcode) %>%
dplyr::select(refLon = .data$longitude, refLat = .data$latitude)
if(nrow(refPoint) == 0) {
warning(“The zip pcode you identified wasn’t located.”)
bad_data : tibble::tibble(
zip = character(),
city = character(), state = character(),
latitude = character(), longitude = character(),
Distance = arithmetic()
)
return(bad_data)
}

# Compute range from mentioned zip to others
radius_earth_miles : 3959
new_zip_points : zipList %>%
select(.data$longitude, .data$latitude)
distance : geosphere::distHaversine(refPoint, new_zip_points, radius_earth_miles)

zipList %>%
dplyr::mutate(Distance = round(!!distance, 2)) %>%
# Filter using unrounded distances
dplyr::filter(!!range is lower than radius)
}

#’ Acquire the US states close to a zip code
#’
#’ code{getStates} yields the list of states that have zip codes which fall
#’ in a stated radius in lower case pattern for utilization in code{choroplethrZip}.
#’ @param zip code The mentioned zip code of which you’d desire the file of zip
#’ codes inside a give radius as type.
#’ @param radius The range in miles from the middle of the provided zip to the
#’ middle of the alternative zips as arithmetic.
#’ @return A character bearing of US state labels, in lower case.
#’ @cases
#’ getStates(“30316”, 100)
#’ @export
getStates : action(zipcode, radius){
data : zipRadius(zipcode, radius)
stateTable : as.data.frame(cbind(datasets::state.abb, tolower(datasets::state.name)))
zipStates : particular(data$state)
States : as.character(stateTable[which(stateTable$V1 %in% zipStates),2])
States
}

#’ acquire the area living by a zip code
#’
#’ code{getZipPop} yields a data grid of zip codes and their area where
#’ the zip codes fall inside a given radius for utilization in code{choroplethrZip}.
#’ @param zip code The mentioned zip code of which you’d desire the file of zip
#’ codes inside a give radius as type.
#’ @param radius The range in miles from the middle of the provided zip to the
#’ middle of the alternative zips as arithmetic.
#’ @yield A data grid with two files.
#’ the range of the code{zipcodes} dispute.
#’ describe{
#’ item{zip}{Character. A zip code close to the mentioned zip code.}
#’ item{population}{Arithmetic. The arae of that zip code.}
#’ }
#’ @cases
#’ getZipPop(“30316”, 10)
#’ @importFrom dplyr select
#’ @importFrom dplyr inner_join
#’ @importFrom dplyr rename
#’ @export
getZipPop : action(zip code, radius){
data : zipRadius(zip code, radius)
data(df_pop_zip)
data %>%
dplyr::select(.data$zip) %>%
dplyr::inner_join(df_pop_zip, by = c(“zip” = “region”)) %>%
dplyr::rename(area = amount)
}

#’ Acquire nearby zip codes
#’
#’ code{getZips} yields the file of zip codes as a character bearing which have
#’ zip codes that fall in a stated radius in lower case arrangement for utilization in
#’ code{choroplethrZip}.
#’ @param zipcode The mentioned zip code of which you’d desire the file of zip
#’ codes inside a give radius as type.
#’ @param radius The range in miles from the middle of the provided zip to the
#’ middle of the alternate zips as arithmetic.
#’ @yield A character bearing of zip codes.
#’ @cases
#’ getZips(“30316”, 10)
#’@export
getZips : action(zipcode, radius){
data : zipRadius(zipcode, radius)
zips : data$zip
zips
}

#’ Search zip code area
#’
#’ Acquire the city, state, latitude and longitude of zip codes
#’ @param zipcodes A character bearing of five digit zip codes
#’ @yield A data grid with five lists and as many tiers as
#’ the range of the code{zipcodes} dispute.
#’ describe{
#’ item{zip}{Type. The zip codes you confirmed to the action.}
#’ item{city}{Type. The cities including the zip codes.}
#’ item{state}{Type. The states including the zip codes.}
#’ item{latitude}{Arithmetic. The latitudes of the middles of the zip codes.}
#’ item{longitude}{Arithmetic. The longitudes of the middles of the zip codes.}
#’ }
#’ @cases
#’ zipcodes : c(“99501”, “90210”, “33162”, “60606”, “42748”, “99999”)
#’ lookupZips(zipcodes)
#’ @importFrom tibble tibble
#’ @importFrom dplyr filter
#’ @export
lookupZips : action(zipcodes) {
tibble::tibble(zip = zipcodes) %>%
dplyr::left_join(zipList, by = “zip”)
}

Scroll to top
Privacy Policy