R/utils-constants.R

Defines functions pct_nonzero szero

Documented in pct_nonzero szero

# need Szero inspired by
# https://geographicdata.science/book/notebooks/04_spatial_weights.html

#' Global sum of weights
#'
#' Calculate the global sum of weights
#' @param wt a weights list—i.e. created by `st_weights()`
#' @returns a scalar numeric
#' @export
#' @examples
#' nb <- st_contiguity(guerry)
#' wt <- st_weights(nb)
#' szero(wt)
szero <- function(wt) {
  sum(unlist(wt))
}

#' Percent Non-zero Neighbors
#'
#' Calculate the percentage of non-zero neighbors in a neighbor list.
#'
#' @param nb a neighbor list object
#' @export
#' @returns a scalar double
#' @examples
#' nb <- st_contiguity(guerry)
#' pct_nonzero(nb)
pct_nonzero <- function(nb) {
  (sum(lengths(nb)) / length(nb)^2) * 100
}

Try the sfdep package in your browser

Any scripts or data that you put into this service are public.

sfdep documentation built on Jan. 11, 2023, 9:08 a.m.