R/EnvType.R

Defines functions EnvType

#' @description Build envirotype marker from delimited cardinal (or quantile) limits of adaptation and developmental stages (or any desired time interval)
EnvType=function(env.variable=NULL,
                 value,
                 output = NULL,
                 stage=NULL,
                 breaks=NULL,
                 data=NULL,
                 round=NULL,
                 nstages = NULL){
  if(is.null(output)){output = "abs"}
  if(is.null(value)){stop("env.variable value is missing")}
  if(is.null(nstages)){nstages <- 5}
  if(is.null(breaks)){breaks =round(seq(from = 0,to = max(value),len=5),0)}
  if(is.null(env.variable)){env.variable="env.variable"}
  if(is.null(round)){round <- 4}
  if(is.null(stage)){
    out=data.frame(table(cut(x = value,breaks = breaks,right = T)))
  }
  else{
    if(!is.factor(stage) & !is.character(stage)){stop("stage must be factor or character")}
    if(is.null(data)){data = data.frame(env.variable=env.variable,value=value,stage=stage)}
    out= melt(plyr::ddply(data,.(stage,env.variable),
                          function(x) table(cut(x = x$value,breaks = breaks,right = T))),
              id.vars=c("stage","env.variable")) 
  }
  if(output == "abs"){out = out;return(out)}
  if(output == "prob"){out$value = round(out$Freq/sum(out$Freq),round);return(out)}
}
gcostaneto/envirotype documentation built on Feb. 19, 2020, 10:36 p.m.