
  # -------------------------------------------------------------------------------------------------------------------------  #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #           File Name              :  replaceLeadingZeros.r                                                                  #
  #           Last Updated Funclist  :  08 Feb 2015,  5:12 AM (Sunday)                                                         #
  #                                                                                                                            #
  #           Author Name            :  Rick Saporta                                                                           #
  #           Author Email           :  RickSaporta@gmail.com                                                                  #
  #           Author URL             :  www.github.com/rsaporta                                                                #
  #                                                                                                                            #
  #           Packages Called        :  NA                                                                                     #
  #           Packages Used via NS   :  NA                                                                                     #
  #                                                                                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  #                                                                                                                            #
  #   replaceLeadingZeros          ( x, replace=NA, keepFirst=TRUE, skip.if.entire.x.is.0=TRUE )                               #
  #   replaceLeadingValuesLessThan ( x, thresh=1e-12, use_abs=FALSE, replace=NA, keepFirst=TRUE, skip.if.entire.x.is.0=TRUE )  #
  #                                                                                                                            #
  #                                                                                                                            #
  #                                                     <END FUNCS>                                                            #
  #  -----------------------------------------------------------------------------------------------------------------------   #
  # -------------------------------------------------------------------------------------------------------------------------  #

# replaceLeadingZeros.r

replaceLeadingZeros <- function(x, replace=NA, keepFirst=TRUE, skip.if.entire.x.is.0=TRUE) {
## replaces leading zeros 
  zeros <- which(x == 0)

  if (length(zeros) == length(x))
      return(x)

  ## The first two must be zero. Also, if ALL are zero, do NOTHING. 
  if (length(zeros) > 1 && zeros[[2]] == 2) {

      ## The first diff to NOT be 1 will correspond to the first zero to *KEEP* (since diff(x) has one index less than x)
      ## We include max(zeros), for the case where the only zeros are the first ones.
      ## If keepFirst, we subtract 1 (aka TRUE) 
      drop_until <- min(which(diff(zeros) != 1), max(zeros)) - keepFirst

      ## ERROR CHECK
      if (!(drop_until >= 1))
        stop ("Internal error.  'drop_until' should be at least 1.")

      ## replace the zeros
      x[1:drop_until] <- replace
  }
  return(x)
}

replaceLeadingValuesLessThan <- function(x, thresh=1e-12, use_abs=FALSE, replace=NA, keepFirst=TRUE, skip.if.entire.x.is.0=TRUE) {
## replaces leading zeros 
  if (use_abs)
    zeros <- which(abs(x) < thresh)
  else 
    zeros <- which(x < thresh)

  if (length(zeros) == length(x))
      return(x)

  ## The first two must be zero. Also, if ALL are zero, do NOTHING. 
  if (length(zeros) > 1 && zeros[[2]] == 2) {

      ## The first diff to NOT be 1 will correspond to the first zero to *KEEP* (since diff(x) has one index less than x)
      ## We include max(zeros), for the case where the only zeros are the first ones.
      ## If keepFirst, we subtract 1 (aka TRUE) 
      drop_until <- min(which(diff(zeros) != 1), max(zeros)) - keepFirst

      ## ERROR CHECK
      if (!(drop_until >= 1))
        stop ("Internal error.  'drop_until' should be at least 1.")

      ## replace the zeros
      x[1:drop_until] <- replace
  }
  return(x)
}
