R/selectExclude.R

Defines functions selectExclude

Documented in selectExclude

#' selectExclude
#' @param dfData a data.frame object or a character string indicating the name of the data.frame object.
#' @param dfColNameVec a string or a vector of class character indicating the name(s) of the column(s) to be excluded.
#' @param suffix a string (default: "excluded") of class character indicating the suffix to be added to the name of the original data.frame object name to obtain the name of the output data.frame object.
#' @param subDir a character string indicating the name of the subdirectory to save the output data.frame object as a .rda file
#' @param envir a variable indicating the environment where the output data.frame object should be saved.
#' @return selectExclude creates a data.frame object containing all columns except the ones specified by \code{dfColNameVec} from the data.frame object \code{dfData} and saves it as a .rda file in the subdirectory called \code{subDir} inside the directory called "input". If the "input" directory is not present in the current working directory, an "input" directory is created and the output .rda file is saved therein. It also saves the data.frame object in the ". GlobalEnv" environment and returns the name of the output data.frame object in the console.
#' @description selectExclude takes as input a date.frame object or a data.frame object name (\code{dfData}), a character vector containing column names (\code{dfColNameVec}), a suffix (\code{suffix}) and the name of the output subdirectory (\code{subDir}) and selects all columns from \code{dfData} except the ones specified in \code{dfColNameVec}. The non-excluded columns are saved as a data.frame object in a .rda file in the \code{subDir} subdirectory in the main directory called "input". If the directory called "input" is not present in the current working directory, a new directory called "input" is created. If a subdirectory is not specified (i.e. missing \code{subDir}), then the output .rda file is saved in input/. The output data.frame object is also saved in the ". GlobalEnv" environment and the name of the output data.frame object is returned in the console.
#' @examples
#' selectExclude(employees,c("emp_id","emp_name","start_date"),"filtered","results")
#' selectExclude(employees,c("emp_id","emp_name","date_of_birth"))
#' @export
selectExclude<-function(dfData,dfColNameVec,suffix,subDir,envir=.GlobalEnv)
{
  if(missing(suffix))
  {
    suffix<-"excluded"
  }
  
  envTab<-new.env()
  
  if(is.character(dfData))
  {
    name<-paste(dfData,"_",suffix,sep="")
    dfData<-get(dfData,envir=envir)
  }
  else
  {
      name<-paste(deparse(substitute(dfData)),"_",suffix,sep="")    
  }
  
  exclude<-loc.col(dfData,dfColNameVec)[2]
  varNames<-names(dfData)[-c(t(exclude))]
  tabVar<-dfData[,varNames]
  
  assign(name,tabVar,envir=.GlobalEnv)
  assign(name,tabVar,envir=envTab)
  
  if (!any(dir(getwd())=="input"))
  {
    print("directory input has been created in the present working directory")
    dir.create("input/",recursive=T)
  }

  # adding subDir
  if(missing(subDir))
  {
    dirInput<-"input"
  }
  else
  {
    dirInput<-file.path("input",subDir,"/")
    if(!dir.exists(dirInput))
    {
      dir.create(dirInput)
    }
  }
  
  save(list=ls(envir=envTab),file=file.path(dirInput,paste(name,".rda",sep="")))
  save(list=ls(envir=envTab),file=(paste("input/",name,".rda",sep="")))
  return(name)
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.