R/pad.R

#------------------------------------------------------------------------------
# pad.R
# Copyright (C) 2019  Geert van Boxtel
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# See also: http://www.gnu.org/licenses/gpl-2.0.txt
#
# Version history:
# 20190114  GvB       Initial setup, no argument checking
#
#------------------------------------------------------------------------------

postpad <- function(x, l, val = 0) {
  
  # # arguments must be atomic type, value should be a scalar (atomic of length 1), l should be a positive scalar
  # if (!is.atomic(x) || !is.atomic(l) || !is.atomic(val)) stop('function postpad: Arguments must be of atomic type')
  # if (length(val) != 1) stop('function postpad: value should be a scalar')
  # if (length(l) != 1 || l <= 0) stop('function postpad: l should be a positive scalar')
  
  lx <- length(x)
  if (l > lx)
    return(c(x, rep(val, l - lx)))
  else
    return(x[1:l])
}

prepad <- function(x, l, val = 0) {
  
  # # arguments must be atomic type, value should be a scalar (atomic of length 1), l should be a positive scalar
  # if (!is.atomic(x) || !is.atomic(l) || !is.atomic(val)) stop('function postpad: Arguments must be of atomic type')
  # if (length(val) != 1) stop('function postpad: value should be a scalar')
  # if (length(l) != 1 || l <= 0) stop('function postpad: l should be a positive scalar')
  
  lx <- length(x)
  if (l > lx)
    return(c(rep(val, l - lx), x))
  else
    return(x[(lx-l+1):lx])
}
gjmvanboxtel/DSPutils documentation built on May 18, 2019, 2:35 p.m.