Skip to contents

Calculate polygon spatial weights from a nb list. See spdep::nb2listw() for further details.

Usage

st_weights(nb, style = "W", allow_zero = NULL, ...)

Arguments

nb

A neighbor list object as created by st_neighbors().

style

Default "W" for row standardized weights. This value can also be "B", "C", "U", "minmax", and "S". See spdep::nb2listw() for details.

allow_zero

If TRUE, assigns zero as lagged value to zone without neighbors.

...

additional arguments passed to spdep::nb2listw().

Value

a list where each element is a numeric vector

Details

Under the hood, st_weights() creates a listw object and then extracts the weights elements from it as the neighbours element is already--presumably--already existent in the neighbors list you've already created. listw objects are recreated using recreate_listw() when calculating other statistics.

See also

Examples

library(magrittr)
guerry %>%
 dplyr::mutate(nb = st_contiguity(geometry),
               wt = st_weights(nb),
               .before = 1)
#> Simple feature collection with 85 features and 28 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 47680 ymin: 1703258 xmax: 1031401 ymax: 2677441
#> CRS:           NA
#> # A tibble: 85 × 29
#>    nb        wt     code_dept count ave_i…¹  dept region depar…² crime…³ crime…⁴
#>  * <nb>      <list> <fct>     <dbl>   <dbl> <int> <fct>  <fct>     <int>   <int>
#>  1 <int [4]> <dbl>  01            1      49     1 E      Ain       28870   15890
#>  2 <int [6]> <dbl>  02            1     812     2 N      Aisne     26226    5521
#>  3 <int [6]> <dbl>  03            1    1418     3 C      Allier    26747    7925
#>  4 <int [4]> <dbl>  04            1    1603     4 E      Basses…   12935    7289
#>  5 <int [3]> <dbl>  05            1    1802     5 E      Hautes…   17488    8174
#>  6 <int [7]> <dbl>  07            1    2249     7 S      Ardeche    9474   10263
#>  7 <int [3]> <dbl>  08            1   35395     8 N      Ardenn…   35203    8847
#>  8 <int [3]> <dbl>  09            1    2526     9 S      Ariege     6173    9597
#>  9 <int [5]> <dbl>  10            1   34410    10 E      Aube      19602    4086
#> 10 <int [5]> <dbl>  11            1    2807    11 S      Aude      15647   10431
#> # … with 75 more rows, 19 more variables: literacy <int>, donations <int>,
#> #   infants <int>, suicides <int>, main_city <ord>, wealth <int>,
#> #   commerce <int>, clergy <int>, crime_parents <int>, infanticide <int>,
#> #   donation_clergy <int>, lottery <int>, desertion <int>, instruction <int>,
#> #   prostitutes <int>, distance <dbl>, area <int>, pop1831 <dbl>,
#> #   geometry <MULTIPOLYGON>, and abbreviated variable names ¹​ave_id_geo,
#> #   ²​department, ³​crime_pers, ⁴​crime_prop
#> # ℹ Use `print(n = ...)` to see more rows, and `colnames()` to see all variable names

# using geometry column directly
nb <- st_contiguity(guerry$geometry)
wt <- st_weights(nb)
wt[1:3]
#> [[1]]
#> [1] 0.25 0.25 0.25 0.25
#> 
#> [[2]]
#> [1] 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
#> 
#> [[3]]
#> [1] 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667 0.1666667
#>