#' @title Merge and arrange raster and shape data frame
#' @author Melody Premaillon
#'
#' @param rast_df Data frame of postprob/frequency (from extract_raster_postprob function)
#' @param shape_df Data frame of postprob/frequency (from extract_shape_postprob function)
#' @param dataset a character "train" or "valid
#'
#' @description This functions merges the two data frames of post prob frequency obtained from extract_raster_postprob and extract_raster_postprob functions. Then it orders it by descending postprob value, compute the cumulated sum of pixels and points and convert it in percent
#'
#' @return a df whith postprob values, number of pixels and points it represents and their cumulated sum and percent.
#' @export
#'
#' @import dplyr
#'
#' @examples
merge_rast_and_shape <- function(rast_df, shape_df, dataset = "train"){
rast_shp_df <- rast_df %>%
left_join(shape_df, by = "POST_PROB") %>%
mutate(POINTS = ifelse(is.na(POINTS), 0, POINTS)) %>%
arrange(desc(POST_PROB)) %>% # arrange by descending post probability value
mutate(count_cum = cumsum(COUNT),
trng_cum = cumsum(POINTS)) %>% # cumsum of pixels and points
mutate(count_cum_prct = count_cum/sum(COUNT)*100,
trng_cum_prct = trng_cum/sum(POINTS)*100)
if (dataset == "valid"){
colnames(rast_shp_df) <- c("POST_PROB", "COUNT", "CONF", "POINTSval", "count_val_cum", "val_cum", "count_cum_val_prct", "val_cum_prct")
}
return(rast_shp_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.