R/apply_pts_by_poly.R

Defines functions apply_pts_by_poly

Documented in apply_pts_by_poly

#' Apply functions over points by polygons
#'
#' This function is a wrapper aroud the `sptools::points_in_polygon` and applies
#' a function `f` to the variable `var` of the `spatialPointsDataFrame` `points`
#' object, by the polygons of `spatialPolygonsDataFrame` `polygons` object.
#'
#' @param points a `SpatialPointsDataFrame` object.
#' @param polygons a `SpatialPolygonsDataFrame` object.
#' @param var the name of the variable of `points` on which to apply the
#' function `f`.
#' @param f the function to be applied.
#' @param ... arguments to passed in `sptools::points_in_polygon`
#'
#' @author Marc Choisy
#'
#' @export
#'
#' @examples
#' library(sf)
#'
#' # SpatialPolygonsDataFrame
#' vn <- sf::as_Spatial(gadmVN::gadm(level = "province"))
#' # SpatialPoints
#' stations <- as(imhen::stations, "Spatial")
#' names(stations)
#'
#' # to calculate the mean elevation of the station for each polygon
#' apply_pts_by_poly(stations, vn, "elevation", mean)
apply_pts_by_poly <- function(points, polygons, var, f, ...) {
  unlist(lapply(seq_along(polygons),
                function(x) f(
                  points_in_polygon(points, polygons[x, ])[[var]], ...)))
}
choisy/sptools documentation built on Aug. 22, 2019, 12:57 p.m.