R/numbersForLevelsAddTable.R

Defines functions numberForLevelsAddTable

Documented in numberForLevelsAddTable

#'numberForLevelsAddTable
#' @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 string or a vector of class character indicating the name(s) of the column(s) of \code{dfData} whose levels have to be numbered.
#' @param vecList a numeric vector or a list of numeric vectors. Each vector contains numbers that are to be assigned to the levels of the respective column. The number of vectors in the list should be equal to the number of column names in \code{dfColNameVec}. The nth list corresponds to the nth column name.
#' @param outputDfName a character string indicating the name of the output data.frame object
#' @param outputDir a character string indicating the storage path for the output .xlsx file
#' @return numberForLevelsAddTable returns a data.frame object and an xlsx file containing a column for each column in \code{dfColNameVec}. In every new column the entries of the corresponding original column are replaced by numbers (as specified by \code{vecList}) assigned to the levels for each column. Additionally, it also prints the output data.frame object in the console window.
#' @description numberForLevelsAddTable takes as input a data.frame object (\code{dfData}), a character vector containg column names (\code{dfColNameVec}), a list of numeric vectors (\code{vecList}), a name for the output data.frame object (\code{outputDfName}) and the storage path (\code{outputDir}). The output is a data.frame object and a .xlsx file containing new columns with numbered levels. The outputs are stored at the location specified by the storage path.
#' @examples
#' numberForLevelsAddTable(mainTable,c("col1","col2"),
#'                         list(c(0,4,3),c(0,1,1.5,2,3)),
#'                         "newTabName","D:/FolderName/")
#' numberForLevelsAddTable(warpbreaks,c("wool","tension"),
#'                         list(c(1:2),c(1,5,10)),
#'                         "df_numberForLevelsAddTable",
#'                         "D:/Work/")
#' numberForLevelsAddTable(iris,"Species",c(1:3),
#'                        "df_numberForLevelsAddTable2",
#'                        "D:/Work/")
#' @export
numberForLevelsAddTable<-function(dfData,dfColNameVec,vecList,outputDfName,outputDir)
{
  numColTab<-data.frame(NA)
  for (i in 1:length(dfColNameVec))
  {
    numCol<-numberForLevels(dfData[,dfColNameVec[i]],
                      levels(factor(dfData[,dfColNameVec[i]])),vecList[[i]])
    names(numCol)<-paste(dfColNameVec[i],".num",sep="")
    numColTab<-cbind(numColTab,numCol)
  }

  numColTab<-numColTab[-loc.col(numColTab,"NA.")[,2]]

  newTab<-cbind(dfData,numColTab)
  assign(outputDfName,newTab,.GlobalEnv)
  print(newTab)
  write.xlsx(newTab, file=paste(outputDir,outputDfName,".xlsx",sep=""),sheetName="numTable")
  save(newTab, file=paste(outputDir,outputDfName,".txt",sep=""))
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.