elbersb / segregation

R package to calculate entropy-based segregation indices, with a focus on the Mutual Information Index (M) and Theil’s Information Index (H)
https://elbersb.com/segregation
Other
35 stars 3 forks source link

Getting this error: "Error in unique.default(x, nmax = nmax) : unique() applies only to vectors" #8

Closed bcongelio closed 3 years ago

bcongelio commented 3 years ago

Hello -

The data I am trying to work with (from tidycensus) looks like this:

# A tibble: 16,542 x 5
   GEOID       variable estimate   moe urban_name 
   <chr>       <chr>       <dbl> <dbl> <chr>      
 1 06083002013 white        2291   331 Santa Maria
 2 06083002013 black           0    13 Santa Maria
 3 06083002013 hispanic      471   246 Santa Maria
 4 06083002011 white        1875   262 Santa Maria
 5 06083002011 black          30    33 Santa Maria
 6 06083002011 hispanic     2720   384 Santa Maria
 7 06061020711 white        3339   420 Roseville  
 8 06061020711 black         117   146 Roseville  
 9 06061020711 hispanic      471   160 Roseville  
10 06061020712 white        2628   338 Roseville  
# ... with 16,532 more rows

And I am trying to run this code using entropy from segregation:

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)

However, doing so creates the following error:

Error in unique.default(x, nmax = nmax) : 
  unique() applies only to vectors

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:

library(tidycensus)
library(segregation)

los.angeles.indices <- get_acs(
  geography = "tract",
  variables = c(
    white = "B03002_003",
    black = "B03002_004",
    hispanic = "B03002_012"
  ), 
  state = "CA",
  geometry = TRUE,
  year = 2012
) 

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")))

ca_city_data <- los.angeles.indices %>%
  st_join(california.cities, left = FALSE) %>%
  select(-NAME) %>%
  st_drop_geometry()
elbersb commented 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)

bcongelio commented 3 years ago

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!

elbersb commented 3 years ago

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).

walkerke commented 3 years ago

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.