Match String Value from Data Frame to Specific Vectors in a List

I'm trying to create a county-level map showing counties where certain events took place, and counties adjacent to them. I have determined adjacency using sf and extracted the adjacent county names into a list of vectors, but I am not quite sure how to impute a variable to my data frame that includes adjacent counties. In plain English, I need a case_when statement that checks each row of nc for a value of anything but 1 in the nc$aliens column, and returns a 1 when the value of nc$NAME on a given row is contained in the vector of neighboring county names of a county whose value is set to 1. For example, my reprex sets Buncombe County to 1, so I would also like to return a 1 for Yancey County because it is adjacent.

library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(dplyr)
#> Warning: package 'dplyr' was built under R version 4.1.3
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot)
#> Error in library(ggplot): there is no package called 'ggplot'

##LOAD NC SHAPEFILE
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

##CALCULATE NEIGHBORING POLYGONS
neighbors <- st_intersects(nc, nc)

##EXTRACT NAMES OF NEIGHBORING POLYGONS/COUNTIES
neighbor_names <- lapply(neighbors, function(x) {nc$NAME[x]})

names(neighbor_names) <- nc$NAME


##ADD COLUMN INDICATING COUNTIES CONTAINING ALIEN CRASH SITES
nc$aliens <- case_when(
      
      nc$NAME == 'Martin'   ~ 1,
      nc$NAME == 'Buncombe' ~ 1,
      TRUE                  ~ 0
)

ggplot()+
      geom_sf(data = nc, aes(fill= factor(aliens)))
#> Error in ggplot(): could not find function "ggplot"
Created on 2023-07-14 with reprex v2.0.2

Hi @liberry! If I understand correctly, I believe this gets to the desired outcome.

library(sf)
library(dplyr)
library(ggplot2)

##LOAD NC SHAPEFILE
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

##CALCULATE NEIGHBORING POLYGONS
neighbors <- st_intersects(nc, nc)

##EXTRACT NAMES OF NEIGHBORING POLYGONS/COUNTIES
neighbor_names <- lapply(neighbors, function(x) {nc$NAME[x]})

neighbor_names = data.frame(NAME = nc$NAME,
                           tibble(neighbor_names)
                           )

# counties of interest
target_counties = c('Martin', 'Buncombe')

# counties adjacent to counties of interest
adjacent_counties = neighbor_names |>
  filter(NAME %in% target_counties) |>
  pull(neighbor_names) |>
  unlist()

# set target counties and adjacent counties to 1
nc = nc |> 
  mutate(aliens = case_when(
    NAME %in% target_counties ~ 1,
    NAME %in% adjacent_counties ~ 1,
    TRUE ~ 0
  )) 

# plot
ggplot()+
  geom_sf(data = nc, aes(fill= factor(aliens)))

Created on 2023-07-16 with reprex v2.0.2

Excellent, thank you.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.