st_as_edges {sfdep} | R 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 |
nb |
a neighbor list. If |
wt |
optional. A weights list as generated by |
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 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 ofj
stored inwt
.
Examples
if (requireNamespace("dplyr", quietly = TRUE)) {
library(magrittr)
guerry %>%
dplyr::mutate(nb = st_contiguity(geometry),
wt = st_weights(nb)) %>%
st_as_edges(nb, wt)
}