R/insertEqLevCol.R

Defines functions insertEqLevCol

Documented in insertEqLevCol

#'insertEqLevCol
#' @param dfData a data.frame object. Note that this is not a character string but the data.frame object itself. The parameter passed to the function should be without quotes (" ").
#' @param dfColNameVec a character string or a vector of class character indicating the name(s) of the column(s) to be handled. 
#' @param numBreaks an integer indicating the number of class divisions (or exact boundaries) that should be taken. The same number of divisions is used for all columns in \code{dfColNameVec}.
#' @return insertEqLevCol updates the data.frame object \code{dfData} by adding new columns containing factor classes corresponding to the entries in the respective columns specified by \code{dfColNameVec} of the original data.frame.
#' @description insertEqLevCol takes as input a data.frame object (\code{dfData}), a vector containing column names (\code{dfColNameVec}), and the number of class divisions (\code{numBreaks}) and inserts a new column in \code{dfData} for each column in \code{dfColNameVec} containing the factor classes corresponding to the entries in that column. 
#' @examples
#' insertEqLevCol(iris,c("Sepal.Length","Sepal.Width"),4)
#' insertEqLevCol(iris,c("Sepal.Length","Petal.Length"),10)
#' @export
insertEqLevCol<-function(dfData,dfColNameVec,numBreaks)
{
  name<-deparse(substitute(dfData))
  for(i in dfColNameVec)
  {
    column<-data.frame(convertToLevels(dfData[,i],8,numBreaks))
    names(column)=paste(i,"F",sep="")
    dfData<-as.data.frame(append(dfData,column,after=(loc.col(dfData,i)[,2])))
  }
  assign(name,dfData,envir=globalenv())
  return(get(name))
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.