st_as_edges | R Documentation |
Given geometry and neighbor and weights lists, create an edge list sf
object.
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)
x |
object of class |
nb |
a neighbor list. If |
wt |
optional. A weights list as generated by |
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
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
.
if (requireNamespace("dplyr", quietly = TRUE)) {
library(magrittr)
guerry %>%
dplyr::mutate(nb = st_contiguity(geometry),
wt = st_weights(nb)) %>%
st_as_edges(nb, wt)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.