R/jDayCol.R

Defines functions jDayCol

Documented in jDayCol

#' jDayCol
#' @param dfData a data.frame object or a character string indicating the name of the data.frame object.
#' @param dfColName a string of class character indicating the name of the column to be handled. The entries in this column must be dates in the date format given by the parameter \code{dateFormat}
#' @param dateFormat a character string indicating the date format of the column \code{dfColName}
#' @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 data.frame object \code{dfData}.  
#' @return jDayCol updates the data.frame object \code{dfData} by adding a new column containing the day of the year corresponding to the entries in column \code{dfColName} of the original data.frame object. The entries in the new column are integers in the range [1,366]
#' @description jDayCol takes as input a date.frame object or a data.frame object name (\code{dfData}), a column name (\code{dfColName}), and the date format (\code{dateFormat}) of the column \code{dfColName} and inserts a new column in the data.frame object containing the day of the year corresponding to the entries in the column \code{dfColName}. The location of the new column is dictated by \code{endRep}.
#' @examples
#' jDayCol("employees","start_date","%Y-%m-%d")
#' jDayCol(students,"date_of_birth","%d.%m.%Y",F)
#' @export
jDayCol<-function(dfData,dfColName,dateFormat,endRep="end")
{
  if(is.character(dfData))
  {
    dfTab<-get(dfData)
  }
  else
  {
    dfTab<-dfData
    tabName<-deparse(substitute(dfData))
  }


  jCol<-data.frame(strptime(dfTab[,dfColName],dateFormat)$yday+1)
  names(jCol)<-paste(dfColName,"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]]))
    }
      
  assign(tabName,tab,envir=globalenv())
  rm(list=c("tab"))
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.