R/orderLevelAlpha.R

Defines functions orderLevelAlpha

Documented in orderLevelAlpha

#'orderLevelAlpha
#' @param dfCol a column of a data.frame object for which the levels are to be displayed. Note that this is not a character string but the column itself (refer to examples)
#' @param numLevels an integer indicating the allowed number of different numeric values
#' @param numBreaks an integer indicating the number of class divisions (or exact boundaries) that should be taken
#' @return orderLevelAlpha returns a data.frame object displaying the levels and level numbers obtained by converting the entries of the column \code{dfCol} into factor classes
#' @description takes as input a column (\code{dfCol}), number of allowed levels (\code{numLevels}), and number of class divisions (\code{numBreaks}) and casts the numeric entries of the column into factor classes
#' @examples
#' orderLevelAlpha(iris[,"Sepal.Length"],5,6)
#' orderLevelAlpha(warpbreaks[,"wool"],2,5)
#' @export
orderLevelAlpha<-function(dfCol,numLevels,numBreaks)
{
  variable<-dfCol

  if(!is.factor(dfCol))
  {
    if(length(levels(as.factor(dfCol)))<numLevels)
      variable<-as.factor(dfCol) else variable<-cut(dfCol,breaks=numBreaks)
  } else variable<-dfCol

  factorConversion<-as.factor(variable)
  colLevels<-data.frame(level=levels(variable))
  ordLevels<-data.frame(numLevel=as.numeric(as.factor(levels(variable))))
  factTable<-cbind(colLevels,ordLevels)
  return(factTable)
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.