R/jDayColsTabs.R

Defines functions jDayColsTabs

Documented in jDayColsTabs

#' jDayColsTabs
#' @param dfNameVec a data.frame object or a vector of class character indicating the name(s) of the data.frame object(s).
#' @param dfColNameVec a string or a vector of class character indicating the name(s) of the column(s) to be handled. The entries in each of these columns are dates in the date format given by the parameter \code{dateFormat}.
#' @param dateFormat a character string indicating the date format of the columns in \code{dfColNameVec}.
#' @param endRep a character string (default: "end") indicating whether to append the new columns at the end (endRep=="end") or replace the original columns with the respective new columns (endRep=="rep") or insert the new columns right after the respective columns (endRep=="after") in the respective data.frame objects.  
#' @return jDayColsTabs updates the data.frame objects in \code{dfNameVec} by adding new columns containing the day of the year corresponding to the entries in the columns specified by \code{dfColNameVec} of the respective original data.frame objects. The entries in the new columns are integers in the range [1,366]
#' @description jDayColsTabs takes as input a character vector containing date.frame object names (\code{dfNameVec}), a character vector containing column names (\code{dfColNameVec}), and the date format (\code{dateFormat}) of the columns and inserts a new column in each data.frame object for each column in \code{dfColNameVec} containing the day of the year corresponding to the entries in that column in the original data.frame object. The location of the new column is dictated by \code{endRep}.
#' @examples
#' jDayColsTabs(c("rice","maize"),c("tillDate","plantDat","hrvDate"),"%d.%m.%Y")
#' jDayColsTabs(c("employees","students2"),c("date_of_birth","start_date"),"%Y-%m-%d",F)
#' jDayColsTabs("employees","date_of_birth","%Y-%m-%d")
#' @export
jDayColsTabs<-function(dfNameVec,dfColNameVec,dateFormat,endRep="end")
{
  if(is.data.frame(dfNameVec)) 
  {

    tabName<-deparse(substitute(dfNameVec))
    tab<-dfNameVec
    for (i in 1:length(dfColNameVec))
    {
      jCol<-data.frame(strptime(dfNameVec[,dfColNameVec[i]],dateFormat)$yday+1)
       names(jCol)<-paste(dfColNameVec[i],"J",sep="")

      if (endRep=="end")
      {
        tab<-cbind(tab,jCol)
      }
      if (endRep=="rep")
      {
        tab[,dfColNameVec[i]]<-jCol
      }
      if (endRep=="after")
      {
        tab<-as.data.frame(append(tab,jCol,after=loc.col(tab,dfColNameVec[i])[[2]]))
      }
      else
      {
        tab<-cbind(tab,jCol)
      }
      
    }

    assign(tabName,tab,envir=globalenv())   
    rm(list=c("tab"))


  }
  
  else
  {
    for (i in dfNameVec)
    {
      for (j in c(dfColNameVec))
      {
        jDayCol(i,j,dateFormat,endRep)
      }
    }
  }

}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.