Learn R Programming

lambda.tools (version 1.0.9)

pad: Pad a vector with some default value

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.

Examples

Run this code
# 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)

Run the code above in your browser using DataLab