Skip to content

Commit

Permalink
use 2020 cities
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherkenny committed Jun 30, 2022
1 parent 7c7a4b4 commit c2b864d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
6 changes: 3 additions & 3 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ NULL

#' U.S. Cities
#'
#' This data contains the location, name, and 2006 population of U.S. cities and
#' This data contains the location, name, and 2020 population of U.S. cities and
#' large towns.
#'
#' @name cities
Expand All @@ -55,8 +55,8 @@ NULL
#' \describe{
#' \item{\code{name}}{City name.}
#' \item{\code{state}}{City state.}
#' \item{\code{pop_2006}}{City population in 2006.}
#' \item{\code{capital}}{TRUE if the city is a capital.}
#' \item{\code{pop_2020}}{City population in 2020}
#' \item{\code{GEOID}}{Census GEOID for the corresponding Census Designated Place.}
#' \item{\code{geometry}}{The `sf` geometry column containing the geographic information.}
#' }
#'
Expand Down
33 changes: 22 additions & 11 deletions data-raw/cities.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,29 @@ library(tigris)
library(stringr)
library(rmapshaper)

raw <- as_tibble(maps::us.cities)
x <- purrr::map_dfr(c(state.abb, 'DC'),
function(st) {
tidycensus::get_decennial('place', 'P2_001N', year = 2020, state = st, geometry = TRUE) %>%
dplyr::mutate(geometry = st_centroid(geometry)) %>%
dplyr::arrange(desc(value)) %>%
dplyr::slice(1:40) %>%
dplyr::mutate(state = st) %>%
dplyr::select(-variable) %>%
dplyr::rename(pop_2020 = value)
})

raw$geometry <- st_as_sf(data.frame(X=raw$long, Y=raw$lat), coords=c("X", "Y")) %>%
st_set_crs(4269) %>%
pull(geometry)
x <- x %>%
dplyr::arrange(desc(pop_2020)) %>%
dplyr::slice(1:1000)

cities <- transmute(raw,
name = str_trim(str_remove(name, country.etc)),
state = country.etc,
pop_2006 = pop,
capital = (capital == 2),
geometry = geometry) %>%
st_as_sf()
x <- x %>%
dplyr::select(
name = NAME, state, GEOID, pop_2020, geometry
)

cities <- x %>%
dplyr::mutate(name = sapply(strsplit(x$name, ','), `[`, 1))

usethis::use_data(cities, overwrite=TRUE, compress="xz")


Binary file modified data/cities.rda
Binary file not shown.
1 change: 0 additions & 1 deletion ggredist.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageCleanBeforeInstall: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
6 changes: 3 additions & 3 deletions man/cities.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c2b864d

Please sign in to comment.