View source: R/user_add_neighbors.R
| add_neighbors | R Documentation |
Modifies adjacency to indicate that neighs they should be treated as neighbors.
add_neighbors(adjacency, neighs)
adjacency |
Adjacency information generated by |
neighs |
A vector of regions to mark as adjacent. Accepts a vector of indices or names assigned to |
add_neighbors() is useful when adjacency information generated by spdep::poly2nb() indicates lone regions without links/neighbors, particularly in island counties such as the Hawaiian islands, Nantucket in Massachusetts, or San Juan in Washington. Note that add_neighbors() marks all listed counties as adjacent, so if you have a set of chaining counties where the first may not be connected to the last, several instances of add_neighbors() will be needed.
A modified adjacency list.
if (requireNamespace("sf", quietly = TRUE) &&
requireNamespace("spdep", quietly = TRUE)) {
mamap <- sf::st_as_sf(mamap[order(mamap$GEOID), ])
ma_adj <- spdep::poly2nb(mamap)
new_neighs <- c(1, 4, 10) # attach regions 1, 4, and 10
ma_adj <- add_neighbors(ma_adj, new_neighs)
# Add neighbors by FIPS code instead of index
ma_adj <- suppressWarnings(spdep::poly2nb(mamap))
names(ma_adj) <- mamap$GEOID
ma_adj <- add_neighbors(ma_adj, neighs = c("25001", "25007", "25019"))
ma_adj <- suppressWarnings(spdep::poly2nb(mamap))
ma_adj <- add_neighbors(ma_adj, c(1, 4)) # only attach 1 and 4
ma_adj <- add_neighbors(ma_adj, c(4, 10)) # only attach 4 and 10
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.