Skip to contents

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

Usage

summarize_aw(
  poly_in = NULL,
  poly_weight = NULL,
  target_fields = NULL,
  id_poly_in = "ID",
  fun = stats::weighted.mean,
  extent = NULL
)

Arguments

poly_in

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

poly_weight

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

target_fields

character. Field names to calculate area-weighted.

id_poly_in

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

fun

function(1). The function to calculate the weighted summary. Default is stats::weighted.mean. The function must have a w argument.

extent

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

Value

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

Note

If poly_in and poly_weight are characters, they will be read as terra::vect objects.

See also

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

Author

Insang Song geoissong@gmail.com

Examples

# package
library(sf)
sf_use_s2(FALSE)
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `/home/runner/work/_temp/Library/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, 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"))

system.time(ppb_nc_aw <- summarize_aw(ppb, nc, c("BIR74", "BIR79"), "id"))
#>    user  system elapsed 
#>   0.674   0.008   0.704 
summary(ppb_nc_aw)
#>        id             BIR74             BIR79        
#>  Min.   :  1.00   Min.   :  265.1   Min.   :  348.3  
#>  1st Qu.: 75.75   1st Qu.: 1919.6   1st Qu.: 2379.6  
#>  Median :150.50   Median : 3146.1   Median : 3863.8  
#>  Mean   :150.50   Mean   : 4027.7   Mean   : 5194.1  
#>  3rd Qu.:225.25   3rd Qu.: 4914.6   3rd Qu.: 6259.5  
#>  Max.   :300.00   Max.   :16721.9   Max.   :23772.3  
#### Example of summarize_aw ends ###