Closed bcongelio closed 3 years ago
I can run your code without problems (see below). Do you have the most recent version of the package installed? It seems, though, that the error is not due to the segregation package, but something else, because there is no unique
call in the entropy
function. So maybe it's a good idea to update the other packages as well and try in a new session. By the way, this package is great for providing fully reproducible examples, I used it to create the code and results below.
library(tidycensus)
library(tidyverse)
library(segregation)
library(tigris)
#> To enable
#> caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1
los.angeles.indices <- get_acs(
geography = "tract",
variables = c(
white = "B03002_003",
black = "B03002_004",
hispanic = "B03002_012"
),
state = "CA",
geometry = TRUE,
year = 2012
)
#> Getting data from the 2008-2012 5-year ACS
#> Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
#> | | | 0% | | | 1% | |= | 1% | |= | 2% | |== | 2% | |== | 3% | |=== | 5% | |==== | 5% | |==== | 6% | |====== | 8% | |====== | 9% | |======= | 9% | |======= | 10% | |======= | 11% | |======== | 11% | |======== | 12% | |========= | 12% | |========= | 13% | |========= | 14% | |========== | 14% | |========== | 15% | |=========== | 15% | |=========== | 16% | |============ | 16% | |============ | 17% | |============= | 18% | |============= | 19% | |============== | 19% | |============== | 20% | |============== | 21% | |=============== | 21% | |=============== | 22% | |================ | 22% | |================ | 23% | |================ | 24% | |================= | 24% | |=================== | 27% | |===================== | 30% | |====================== | 31% | |====================== | 32% | |======================= | 33% | |=========================== | 38% | |============================ | 40% | |============================ | 41% | |============================= | 41% | |============================= | 42% | |============================== | 42% | |============================== | 43% | |============================== | 44% | |=============================== | 44% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |======================================== | 58% | |========================================== | 61% | |=========================================== | 61% | |=========================================== | 62% | |=============================================== | 67% | |================================================ | 68% | |================================================ | 69% | |================================================= | 69% | |================================================= | 70% | |================================================= | 71% | |================================================== | 71% | |================================================== | 72% | |====================================================== | 77% | |======================================================= | 78% | |======================================================= | 79% | |======================================================== | 79% | |======================================================== | 80% | |======================================================== | 81% | |========================================================= | 81% | |========================================================= | 82% | |========================================================== | 82% | |========================================================== | 83% | |========================================================== | 84% | |=========================================================== | 84% | |=========================================================== | 85% | |============================================================ | 85% | |============================================================ | 86% | |=============================================================== | 90% | |================================================================= | 93% | |================================================================= | 94% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 100%
california.cities <- get_acs(
geography = "place",
state = "CA",
variables = "B01001_001",
geometry = TRUE,
year = 2012,
survey = "acs1"
) %>%
filter(estimate >= 100000) %>%
transmute(urban_name = str_remove(NAME,
fixed(" city, California")))
#> The 1-year ACS provides data for geographies with populations of 65,000 and greater.
#> Getting data from the 2012 1-year ACS
#> Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
#> | | | 0% | |=========== | 15% | |============ | 18% | |============= | 18% | |======================== | 34% | |=============================== | 45% | |================================ | 45% | |================================ | 46% | |================================= | 47% | |================================= | 48% | |================================== | 48% | |================================== | 49% | |=================================== | 49% | |=================================== | 50% | |=================================== | 51% | |==================================== | 51% | |==================================== | 52% | |===================================== | 52% | |===================================== | 53% | |===================================== | 54% | |====================================== | 54% | |====================================== | 55% | |======================================= | 55% | |======================================= | 56% | |======================================== | 56% | |======================================== | 57% | |======================================== | 58% | |========================================= | 58% | |========================================= | 59% | |========================================== | 59% | |========================================== | 60% | |====================================================== | 77% | |================================================================ | 91% | |================================================================ | 92% | |================================================================= | 92% | |================================================================= | 93% | |================================================================== | 94% | |================================================================== | 95% | |=================================================================== | 95% | |=================================================================== | 96% | |==================================================================== | 96% | |==================================================================== | 97% | |==================================================================== | 98% | |===================================================================== | 98% | |===================================================================== | 99% | |======================================================================| 99% | |======================================================================| 100%
ca_city_data <- los.angeles.indices %>%
st_join(california.cities, left = FALSE) %>%
select(-NAME) %>%
st_drop_geometry()
inglewood_entropy <- ca_city_data %>%
filter(urban_name == "Inglewood") %>%
split(~GEOID) %>%
map_dbl(~{
entropy(
data = .x,
group = "variable",
weight = "estimate",
base = 4
)
}) %>%
as_tibble(rownames = "GEOID") %>%
rename(entropy = value)
inglewood_entropy
#> # A tibble: 50 × 2
#> GEOID entropy
#> <chr> <dbl>
#> 1 06037234902 0.576
#> 2 06037235100 0.453
#> 3 06037235201 0.548
#> 4 06037235202 0.550
#> 5 06037237900 0.357
#> 6 06037238000 0.420
#> 7 06037238100 0.421
#> 8 06037238400 0.430
#> 9 06037276100 0.590
#> 10 06037277100 0.506
#> # … with 40 more rows
Created on 2021-09-02 by the reprex package (v2.0.1)
Hmmm ... very interesting that you have no problem running it. I just updated all packages, restarted R, etc. and still getting the error. But, as you said, it is not a segregation
package issue since unique()
is base R. Thanks for helping lead me in the right direction!
It sounds like a package conflict issue, but if you tried in a new environment, that's really odd that is doesn't work. If you find out what's wrong, feel free to post the solution here -- that way it might be useful for someone else (and maybe it ends up being a segregation issue after all).
Hi @bcongelio and @elbersb - this is because I'm using new features in split()
in R 4.1 in the book. The old notation for split()
would be split(.$GEOID)
; as of R 4.1 you can do split(~GEOID)
. I'd recommend updating to R 4.1 and it should work.
Hello -
The data I am trying to work with (from
tidycensus
) looks like this:And I am trying to run this code using
entropy
fromsegregation
:However, doing so creates the following error:
I've been tinkering with this for quite some time to try to solve the issue but can't seem to figure it out. Any thoughts?
QUICK EDIT:
This is the
tidycensus
code I am using to create the DF: