#' Summarize fields in \code{bfastSpatial()} output
#'
#' This function manipulates the data frame produced by \code{bfastSpatial} to
#' provide summary statistics for a specified field.
#'
#' This function may be used for zonal statistics if combined with
#' \code{subset_bfast()}.
#'
#' @param bfast_in A data frame generated by \code{bfastSpatial()}.
#' @param stat A character containing the name of a field in \code{bfast_in}.
#' @param fun1 A function that returns a single value and accepts an
#' \code{na.rm} argument. This argument summarizes the values in \code{stat}
#' by cell number.
#' @param fun2 A function that returns a single value and accepts an
#' \code{na.rm} argument. This argument summarizes the results of the
#' application of \code{fun1}.
#' @param st_date A Date indicating the earliest timing of observations to
#' include in the summary.
#' @param ed_date A Date indicating the latest timing of observations to inclide
#' in the summary.
#' @return A number that summarizes \code{bfast_in} with \code{fun1} and
#' \code{fun2}.
#' @examples
#' \dontrun{
#' summary_stat(bf_df, "brk", sum, mean)
#' }
#' @importFrom rlang !! sym
#' @importFrom dplyr filter group_by summarise
#' @export
summary_stat <- function(bfast_in, stat, fun1, fun2, st_date=-Inf, ed_date=Inf) {
stat_df <- bfast_in %>%
clean_bfast() %>%
filter(start_date>=st_date, start_date<=ed_date, !! sym(stat)!=-9999) %>%
group_by(no_cell) %>%
summarise(computed=fun1(!! sym(stat), na.rm=TRUE)) %>%
as.data.frame()
return(fun2(stat_df[,"computed"], na.rm=TRUE))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.