tmap -赋值数据“as.numeric(...)”必须与现有数据兼容

ql3eal8s  于 2023-03-15  发布在  其他
关注(0)|答案(1)|浏览(127)

我正在检查纽约州门罗县的种族。我得到了一个错误代码:!分配的数据as.numeric(...)必须与现有数据兼容,能否帮助我解决这个问题?先谢了,

以下是我使用的R代码:

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

但我得到了下面的信息:

错误:!分配的数据as.numeric(...)必须与现有数据兼容。现有数据有211行。分配的数据有210行。仅回收大小为1的向量。运行rlang::last_error()查看错误发生的位置。警告消息:形状门罗_black包含空单位。

ax6ht2ek

ax6ht2ek1#

最后一行包含空几何图形:

删除该行将给予预期的结果:

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

相关问题