st_as_edges: Convert to an edge lines object

View source: R/sfnetworks.R

st_as_edgesR Documentation

Convert to an edge lines object

Description

Given geometry and neighbor and weights lists, create an edge list sf object.

Usage

st_as_edges(x, nb, wt)

## S3 method for class 'sf'
st_as_edges(x, nb, wt)

## S3 method for class 'sfc'
st_as_edges(x, nb, wt)

Arguments

x

object of class sf or sfc.

nb

a neighbor list. If x is class sf, the unquote named of the column. If x is class sfc, an object of class nb as created from st_contiguity().

wt

optional. A weights list as generated by st_weights(). . If x is class sf, the unquote named of the column. If x is class sfc, the weights list itself.

Details

Creating an edge list creates a column for each i position and j between an observation and their neighbors. You can recreate these values by expanding the nb and wt list columns.

library(magrittr)
guerry_nb %>%
  tibble::as_tibble() %>%
  dplyr::select(nb, wt) %>%
  dplyr::mutate(i = dplyr::row_number(), .before = 1) %>%
  tidyr::unnest(c(nb, wt))
#> # A tibble: 420 x 3
#>        i    nb    wt
#>    <int> <int> <dbl>
#>  1     1    36 0.25 
#>  2     1    37 0.25 
#>  3     1    67 0.25 
#>  4     1    69 0.25 
#>  5     2     7 0.167
#>  6     2    49 0.167
#>  7     2    57 0.167
#>  8     2    58 0.167
#>  9     2    73 0.167
#> 10     2    76 0.167
#> # i 410 more rows

Value

Returns an sf object with edges represented as a LINESTRING.

  • from: node index. This is the row position of x.

  • to: node index. This is the neighbor value stored in nb.

  • i: node index. This is the row position of x.

  • j: node index. This is the neighbor value stored in nb.

  • wt: the weight value of j stored in wt.

Examples


if (requireNamespace("dplyr", quietly = TRUE)) {

library(magrittr)
guerry %>%
  dplyr::mutate(nb = st_contiguity(geometry),
         wt = st_weights(nb)) %>%
  st_as_edges(nb, wt)

}

sfdep documentation built on Sept. 13, 2024, 5:07 p.m.