I'd like to keep the census tracts that comprise a larger census geography. The tracts should fit within the larger geography perfectly.
I can do this with a negative buffer, but is there a better way to do this?
library(tidyverse)
library(sf)
library(tidycensus)
library(tigris)
library(tmap)
balt <- places("Maryland",
year = 2018,
class = "sf") %>%
filter(NAME == "Baltimore") %>%
st_transform(4326)
balt_tracts <- get_acs(geography = "tract",
variables = "B01003_001E", # population
year = 2018,
state = "Maryland",
survey = "acs5",
geometry = TRUE) %>%
st_transform(4326)
balt_tracts %>%
.[st_within(st_buffer(., -0.001),
balt) %>% lengths > 0,] %>%
qtm()
An intersection returns tracts outside the larger geography that share an edge, which I don't want.
balt_tracts[balt,] %>%
qtm()
Edit
I'm looking for a way to take this result of st_overlaps() and keep only those polygons that are inside the boundary (balt).
balt_tracts[st_overlaps(balt_tracts, balt) %>% lengths > 0,] %>%
tm_shape() + tm_polygons(col = "GEOID")
One thing I did notice is that st_touches() returns only three polygons. Is this a precision issue, or does this mean the census tract borders are not actually perfectly aligned with the larger polygon's borders?
balt_tracts[st_touches(balt_tracts, balt) %>% lengths > 0,] %>%
tm_shape() + tm_polygons(col = "GEOID")






