Jack
Jack

Reputation: 11

tmap - Assigned data `as.numeric(...)` must be compatible with existing data

I am examining race in Monroe County, NY. I got an error code: ! Assigned data as.numeric(...) must be compatible with existing data. Can you please help me address this issue? Thank you in advance,

#The following is the R code I have used:

monroe_race <- get_decennial(
  geography = "tract",
  state = "NY",
  county = "Monroe",
  variables = c(
    Hispanic = "P2_002N",
    White = "P2_005N",
    Black = "P2_006N",
    Native = "P2_007N",
    Asian = "P2_008N"
  ),
  summary_var = "P2_001N",
  year = 2020,
  geometry = TRUE
) %>%
  mutate(percent = 100 * (value / summary_value))

monroe_black <- filter(monroe_race, 
                         variable == "Black")

tm_shape(shp = monroe_black) + 
  tm_polygons()

#But I got the message below:

Error: ! Assigned data as.numeric(...) must be compatible with existing data. ✖ Existing data has 211 rows. ✖ Assigned data has 210 rows. ℹ Only vectors of size 1 are recycled. Run rlang::last_error() to see where the error occurred. Warning message: The shape monroe_black contains empty units.

Upvotes: 1

Views: 522

Answers (1)

mgrund
mgrund

Reputation: 1625

The last row contains an empty geometry:

enter image description here

Removing that row will give you the expected result:

library(tidyverse)
library(sf)
library(tidycensus)
library(tmap)

monroe_race <- get_decennial(
  geography = "tract",
  state = "NY",
  county = "Monroe",
  variables = c(
    Hispanic = "P2_002N",
    White = "P2_005N",
    Black = "P2_006N",
    Native = "P2_007N",
    Asian = "P2_008N"
  ),
  summary_var = "P2_001N",
  year = 2020,
  geometry = TRUE
) %>%
  mutate(percent = 100 * (value / summary_value))

monroe_black <- filter(monroe_race, 
                         variable == "Black")

# remove empty geometry
monroe_black_filt <- monroe_black %>% filter(!st_is_empty(monroe_black))

tm_shape(shp = monroe_black_filt) + 
  tm_polygons()

enter image description here

Upvotes: 1

Related Questions