R/equalLengthTables.R

Defines functions equalLengthTables

Documented in equalLengthTables

#'equalLengthTables
#' @param shorterDf  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 longerDf  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 (" ").
#' @return equalLengthTables returns a data.frame object with common elements replaced with the values in the longer data.frame object \code{longerDf}.
#' @description equalLengthTables equalLengthTables takes as input two data.frame objects and returns a data.frame object with common values present in both data.frame objects. It replaces the values with the values of \code{longerDf}.
#' @examples 
#' short_table <-data.frame(c(1,4,5,6,8),"short_val")
#' long_table <- data.frame(c(1,2,4,5,6,7,8,9),"long_val")
#' equalLengthTables(short_table,long_table)
#' employees_short <- employees[c(2,3,5),]
#' equalLengthTables(employees_short,employees)
#' iris_short <- iris[c(50:75),]
#' equalLengthTables(iris_short,iris)
#' @export
equalLengthTables<-function(shorterDf,longerDf)
{
  longerTabModified<-data.frame()
  add<-0

  for (i in 1:length(longerDf[,1]))
  {
    j<-i-add
    
    if (isTRUE(longerDf[i,1]==shorterDf[j,1]))
    {
      rowTable<-longerDf[i,]
      longerTabModified<-rbind(longerTabModified,rowTable)
    }
    else 
    {
      add<-add+1
    }
  }
  return(longerTabModified)
}
lwTools/agriTrf documentation built on March 26, 2020, 12:09 a.m.