build_neighbors: Build a Spatial Neighborhood

View source: R/build_neighbors.R

build_neighborsR Documentation

Build a Spatial Neighborhood

Description

Identifies neighboring observations or unique spatial locations for a focal observation.

Usage

build_neighbors(
  coords,
  focal_index,
  bandwidth,
  adaptive = TRUE,
  neighbor_unit = c("row", "location"),
  location_id = NULL
)

Arguments

coords

Numeric matrix or data frame containing spatial coordinates.

focal_index

Integer index identifying the focal observation.

bandwidth

Numeric bandwidth. For adaptive neighborhoods, this is the number of neighboring rows or unique locations.

adaptive

Logical indicating whether an adaptive neighborhood is used.

neighbor_unit

Character string indicating whether neighborhoods are defined using individual data rows ("row") or unique spatial locations ("location").

location_id

Optional vector identifying the spatial location associated with each observation. Required when neighbor_unit = "location".

Value

A list describing the spatial neighborhood of the focal observation. The list contains:

neighbor_index

Integer vector giving the rows of coords included in the local neighborhood.

distances

Numeric vector containing the distance from the focal location to each selected neighboring row.

local_bandwidth

Numeric value giving the realized local bandwidth. For adaptive neighborhoods this is the distance to the most distant selected neighbor; for fixed neighborhoods it is the supplied bandwidth.

all_distances

Numeric vector of distances from the focal location to all candidate rows or unique locations, depending on neighbor_unit.

neighbor_unit

Character string indicating whether the neighborhood was constructed using rows or unique locations.

neighbor_location_id

For location-based neighborhoods, the identifiers of the selected unique locations; NULL for row-based neighborhoods.

Examples

coords <- matrix(
  c(0, 0,
    1, 0,
    2, 0,
    3, 0,
    4, 0),
  ncol = 2,
  byrow = TRUE
)

build_neighbors(
  coords = coords,
  focal_index = 3,
  bandwidth = 3,
  adaptive = TRUE
)

gwrf documentation built on Aug. 24, 2026, 5:15 p.m.