R/fcs_combine.R

Defines functions fcs_combine

Documented in fcs_combine

#' @title Combining Ex-situ and In-situ gap analysis results in one comprehensive score
#' @name fcs_combine
#' @description Concatenates the SRSin, GRSin, ERSin, SRSex, GRSex, ERSex and Final conservation scores
#'  in one unique CSV file with a combined conservation score using the minimun, average and maximum values between FCSin and FCSex scores
#'  and provides final conservation categories based on the scores above mentioned.
#'
#' @param species A name species compiled using '_'  to call occurrences files from Workspace/parameter/occurrences folder
#' @param Workspace A forder where the pipeline will be executed
#' @param  run_version The version of the analysis used (e.g 'v1')
#'
#' @return It returns a data frame file saved at gap_analysis folder with nive fields:
#'
#' \tabular{lcc}{
#'  ID \tab Species name \cr
#'  FCSex \tab Ex-situ final conservation score \cr
#'  FCSin \tab In-situ final conservation score \cr
#'  FCSc_min \tab Final conservation score mininum value between FCSin and FCSex \cr
#'  FCSc_max \tab Final conservation score maximum value between FCSin and FCSex \cr
#'  FCSc_mean \tab Final conservation score average value between FCSin and FCSex \cr
#'  FCSc_min_class \tab Final conservation category using  FCSc_min value \cr
#'  FCSc_max_class \tab Final conservation category using  FCSc_max value \cr
#'  FCSc_mean_class \tab Final conservation category using  FCSc_mean value \cr
#' }
#'
#' @examples fcs_combine('Cucurbita_digitata',Workspace,'v1')
#'
#' Workspace  <-  'E:/CIAT/workspace/Workspace_test/workspace'
#' run_version  <- 'v1'
#' species_list <- c('Cucurbita_cordata',
#'  'Cucurbita_digitata',
#'  'Cucurbita_foetidissima',
#'  'Cucurbita_palmata')
#'
#'  run_version <-'v1'
#
#' lapply(1:length(species_list),function(i){
#'    species <- species_list[[i]]
#'    x <- fcs_combine(species,Workspace,run_version)
#'    print(paste0(species,' DONE!'))
#' })
#'
#'@references
#'
#' Khoury, C. K., Amariles, D., Soto, J. S., Diaz, M. V., Sotelo, S., Sosa, C. C., … Jarvis, A. (2019).
#' Comprehensiveness of conservation of useful wild plants: An operational indicator for biodiversity
#' and sustainable development targets. Ecological Indicators. https://doi.org/10.1016/j.ecolind.2018.11.016
#'
#' @export

fcs_combine <- function(fcsEx, fcsIn) {

  #importFrom("methods", "as")
  #importFrom("stats", "complete.cases", "filter", "median")
  #importFrom("utils", "data", "memory.limit", "read.csv", "write.csv")

  #join datasets and select necessary Columns
  df <- dplyr::left_join(x = fcsEx, y = fcsIn, by = "species") %>%
    dplyr::select("species", "FCSex", "FCSin")


  for(i in 1:nrow(df)){
    #compute FCSc_min and FCSc_max
    df$FCSc_min[i] <- min(c(df$FCSex[i],df$FCSin[i]),na.rm=T)
    df$FCSc_max[i] <- max(c(df$FCSex[i],df$FCSin[i]),na.rm=T)
    df$FCSc_mean[i] <- mean(c(df$FCSex[i],df$FCSin[i]),na.rm=T)

    #assign classes (min)
    if (df$FCSc_min[i] < 25) {
      df$FCSc_min_class[i] <- "HP"
    } else if (df$FCSc_min[i] >= 25 & df$FCSc_min[i] < 50) {
      df$FCSc_min_class[i] <- "MP"
    } else if (df$FCSc_min[i] >= 50 & df$FCSc_min[i] < 75) {
      df$FCSc_min_class[i] <- "LP"
    } else {
      df$FCSc_min_class[i] <- "SC"
    }

    #assign classes (max)
    if (df$FCSc_max[i] < 25) {
      df$FCSc_max_class[i] <- "HP"
    } else if (df$FCSc_max[i] >= 25 & df$FCSc_max[i] < 50) {
      df$FCSc_max_class[i] <- "MP"
    } else if (df$FCSc_max[i] >= 50 & df$FCSc_max[i] < 75) {
      df$FCSc_max_class[i] <- "LP"
    } else {
      df$FCSc_max_class[i] <- "SC"
    }

    #assign classes (mean)
    if (df$FCSc_mean[i] < 25) {
      df$FCSc_mean_class[i] <- "HP"
    } else if (df$FCSc_mean[i] >= 25 & df$FCSc_mean[i] < 50) {
      df$FCSc_mean_class[i] <- "MP"
    } else if (df$FCSc_mean[i] >= 50 & df$FCSc_mean[i] < 75) {
      df$FCSc_mean_class[i] <- "LP"
    } else {
      df$FCSc_mean_class[i] <- "SC"
    }

  }
  return(df)
}

# Workspace = "E:/CIAT/workspace/Workspace_test/workspace"
# run_version="v1"
# species_list <- c(
#   "Cucurbita_cordata",
#   "Cucurbita_digitata",
#   "Cucurbita_foetidissima",
#   "Cucurbita_palmata"
# )
# run_version <-"v1"
#
# lapply(1:length(species_list),function(i){
#   species <- species_list[[i]]
#   x <- fcs_combine(species,Workspace,run_version)
#   cat(paste0(species," DONE!"),"\n")
# })
dcarver1/gapAnalysisR documentation built on Feb. 29, 2020, 12:13 p.m.