pad: Pad a vector with some default value

Description Arguments Value Usage Details Author(s) Examples

Description

This function pads a vector with default values as a way to coerce the value to some predetermined length.

Arguments

x

A vector to pad

head

The amount to prepend

tail

The amount to append

default

The value to use for the pad

Value

A padded sequence

Usage

pad(x, head, tail=0, default=NA)

Details

It is common for sequence operations to return a sequence that is shorter than the original sequence. This phenomenon can be annoying when binding the output with the input in a regular data structure like a matrix or data.frame. This function prepends or appends a specified value to a data structure to ensure that the length of the data structure is compatible with another data structure.

Author(s)

Brian Lee Yung Rowe

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# A moving average results in n - window + 1 results, so pad at the
# head to get a vector of length 50
x <- abs(rnorm(50))
m <- maprange(x, 10, mean)
pad(m, 9) 

# Pad at the end instead of the beginning. Note that the head must
# explicitly be set to 0
pad(m, 0, 9)

# Pad on both sides
pad(m, 4, 5) 

# Use a different default value
pad(m, 9, default=0)

zatonovo/lambda.tools documentation built on May 4, 2019, 9:11 p.m.