Skip to contents

When x and y are different classes, poly_weight will be converted to the class of x.

Usage

summarize_aw(x, y, ...)

# S4 method for class 'SpatVector,SpatVector'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = stats::weighted.mean,
  extent = NULL,
  ...
)

# S4 method for class 'character,character'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = stats::weighted.mean,
  out_class = "terra",
  extent = NULL,
  ...
)

# S4 method for class 'sf,sf'
summarize_aw(
  x,
  y,
  target_fields = NULL,
  id_x = "ID",
  fun = NULL,
  extent = NULL,
  ...
)

Arguments

x

A sf/SpatVector object or file path of polygons detectable with GDAL driver at weighted means will be calculated.

y

A sf/SpatVector object or file path of polygons from which weighted means will be calculated.

...

Additional arguments depending on class of x and y.

target_fields

character. Field names to calculate area-weighted.

id_x

character(1). The unique identifier of each polygon in x. Default is "ID".

fun

function(1)/character(1). The function to calculate the weighted summary. Default is stats::weighted.mean. The function must have a w argument. If both x and y are sf, it should be one of c("sum", "mean"). It will determine extensive argument in sf::st_interpolate_aw.

extent

numeric(4) or SpatExtent object. Extent of clipping x. It only works with x of character(1) file path. See terra::ext for more details. Coordinate systems should match.

out_class

character(1). "sf" or "terra". Output class.

Value

A data.frame with all numeric fields of area-weighted means.

Note

x and y classes should match. If x and y are characters, they will be read as sf objects.

See also

Other Macros for calculation: extract_at(), kernelfunction(), summarize_sedc()

Author

Insang Song geoissong@gmail.com

Examples

# package
library(sf)
options(sf_use_s2 = FALSE)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `/github/home/R/x86_64-pc-linux-gnu-library/4.5/sf/shape/nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27
nc <- sf::st_transform(nc, "EPSG:5070")
pp <- sf::st_sample(nc, size = 300)
pp <- sf::st_as_sf(pp)
pp[["id"]] <- seq(1, nrow(pp))
sf::st_crs(pp) <- "EPSG:5070"
ppb <- sf::st_buffer(pp, nQuadSegs=180, dist = units::set_units(20, "km"))

suppressWarnings(
  ppb_nc_aw <-
    summarize_aw(
      ppb, nc, c("BIR74", "BIR79"),
      "id", fun = "sum"
    )
)
summary(ppb_nc_aw)
#>      BIR74             BIR79                  geometry  
#>  Min.   :  190.4   Min.   :  240.6   POLYGON      :300  
#>  1st Qu.: 1485.1   1st Qu.: 1885.1   epsg:5070    :  0  
#>  Median : 2569.6   Median : 3155.8   +proj=aea ...:  0  
#>  Mean   : 3164.8   Mean   : 4063.4                      
#>  3rd Qu.: 4031.6   3rd Qu.: 5207.2                      
#>  Max.   :15421.6   Max.   :21919.8                      

# terra examples
library(terra)
ncpath <- system.file("gpkg/nc.gpkg", package = "sf")
nc <- terra::vect(ncpath)
pp <- terra::spatSample(nc, size = 300)
pp[["id"]] <- seq(1, nrow(pp))
ppb <- terra::buffer(pp, 20000)

suppressWarnings(
  ppb_nc_aw <-
    summarize_aw(
      ppb, nc, c("BIR74", "BIR79"), "id",
      fun = sum
    )
)
#> Error in dplyr::summarize(dplyr::group_by(data.frame(poly_intersected),     !!rlang::sym(id_x)), dplyr::across(dplyr::all_of(target_fields),     ~fun(., w = area_segment_))):  In argument: `dplyr::across(dplyr::all_of(target_fields), ~fun(., w =
#>   area_segment_))`.
#> Caused by error in `across()`:
#>  In argument: `dplyr::all_of(target_fields)`.
#> Caused by error in `dplyr::all_of()`:
#> ! Can't subset elements that don't exist.
#>  Elements `BIR74` and `BIR79` don't exist.
summary(ppb_nc_aw)
#>      BIR74             BIR79                  geometry  
#>  Min.   :  190.4   Min.   :  240.6   POLYGON      :300  
#>  1st Qu.: 1485.1   1st Qu.: 1885.1   epsg:5070    :  0  
#>  Median : 2569.6   Median : 3155.8   +proj=aea ...:  0  
#>  Mean   : 3164.8   Mean   : 4063.4                      
#>  3rd Qu.: 4031.6   3rd Qu.: 5207.2                      
#>  Max.   :15421.6   Max.   :21919.8