Given geometry and neighbor and weights lists, create an edge list sf object.
Usage
st_as_edges(x, nb, wt)
# S3 method for sf
st_as_edges(x, nb, wt)
# S3 method for sfc
st_as_edges(x, nb, wt)Arguments
- x
object of class
sforsfc.- nb
a neighbor list. If
xis classsf, the unquote named of the column. Ifxis classsfc, an object of classnbas created fromst_contiguity().- wt
optional. A weights list as generated by
st_weights(). . Ifxis classsf, the unquote named of the column. Ifxis classsfc, the weights list itself.
Value
Returns an sf object with edges represented as a LINESTRING.
from: node index. This is the row position ofx.to: node index. This is the neighbor value stored innb.i: node index. This is the row position ofx.j: node index. This is the neighbor value stored innb.wt: the weight value ofjstored inwt.
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 rowsExamples
if (requireNamespace("dplyr", quietly = TRUE)) {
library(magrittr)
guerry %>%
dplyr::mutate(nb = st_contiguity(geometry),
wt = st_weights(nb)) %>%
st_as_edges(nb, wt)
}
#> Simple feature collection with 420 features and 5 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 143129.7 ymin: 1735692 xmax: 983300.8 ymax: 2615768
#> CRS: NA
#> First 10 features:
#> from to i j wt geometry
#> 1 1 36 1 36 0.2500000 LINESTRING (827911.9 212290...
#> 2 1 37 1 37 0.2500000 LINESTRING (827911.9 212290...
#> 3 1 67 1 67 0.2500000 LINESTRING (827911.9 212290...
#> 4 1 69 1 69 0.2500000 LINESTRING (827911.9 212290...
#> 5 2 7 2 7 0.1666667 LINESTRING (691725.7 249605...
#> 6 2 49 2 49 0.1666667 LINESTRING (691725.7 249605...
#> 7 2 57 2 57 0.1666667 LINESTRING (691725.7 249605...
#> 8 2 58 2 58 0.1666667 LINESTRING (691725.7 249605...
#> 9 2 73 2 73 0.1666667 LINESTRING (691725.7 249605...
#> 10 2 76 2 76 0.1666667 LINESTRING (691725.7 249605...