Learn R Programming

spatialrisk (version 0.8.1)

summarise_points_by_polygon: Summarise point values by polygon

Description

Spatially joins point data to polygon geometries and summarises a numeric point attribute for each polygon.

Usage

summarise_points_by_polygon(
  polygons,
  points,
  value,
  fun = sum,
  lon = "lon",
  lat = "lat",
  crs = 4326,
  output_col = NULL,
  na.rm = TRUE,
  outside = c("message", "warning", "ignore"),
  repair_geometry = TRUE
)

Value

An sf object equal to polygons with an additional summary column.

Arguments

polygons

An object of class sf containing polygon geometries.

points

A data.frame containing point coordinates and the value to summarise.

value

A string giving the name of the numeric column in points to summarise.

fun

A summary function, such as sum, mean, or length. Default is sum.

lon

A string with the name of the longitude column in points. Default is "lon".

lat

A string with the name of the latitude column in points. Default is "lat".

crs

Coordinate reference system of the point coordinates. Default is 4326.

output_col

Optional string giving the name of the output column. If NULL, the name is created from value and fun, for example "amount_sum" or "amount_mean".

na.rm

Logical. Whether to remove missing values when fun supports an na.rm argument. Default is TRUE.

outside

What to do when points fall outside all polygons: "message" (default), "warning", or "ignore".

repair_geometry

Logical. Whether to try sf::st_buffer(x, 0) if transforming polygon geometries fails. Default is TRUE.

Examples

Run this code
summarise_points_by_polygon(
  polygons = nl_postcode2,
  points = insurance,
  value = "amount",
  fun = sum
)

Run the code above in your browser using DataLab