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)

Example output

 [1]        NA        NA        NA        NA        NA        NA        NA
 [8]        NA        NA 0.7759501 0.8255090 0.7120667 0.6951577 0.7542180
[15] 0.7275316 0.7451503 0.7285545 0.7469617 0.8297034 0.7741526 0.7519351
[22] 0.7644968 0.7729314 0.7364307 0.7507919 0.7464788 0.8685851 0.9373643
[29] 0.8237068 0.7730725 0.7252644 0.6746505 0.6903091 0.7075686 1.0142540
[36] 1.2522175 1.2396196 1.1698218 1.2490693 1.1954656 1.2237815 1.2674711
[43] 1.1768190 1.2584819 0.9966886 0.8634196 0.7858126 0.9126845 0.8671883
[50] 0.9990682
 [1] 0.7759501 0.8255090 0.7120667 0.6951577 0.7542180 0.7275316 0.7451503
 [8] 0.7285545 0.7469617 0.8297034 0.7741526 0.7519351 0.7644968 0.7729314
[15] 0.7364307 0.7507919 0.7464788 0.8685851 0.9373643 0.8237068 0.7730725
[22] 0.7252644 0.6746505 0.6903091 0.7075686 1.0142540 1.2522175 1.2396196
[29] 1.1698218 1.2490693 1.1954656 1.2237815 1.2674711 1.1768190 1.2584819
[36] 0.9966886 0.8634196 0.7858126 0.9126845 0.8671883 0.9990682        NA
[43]        NA        NA        NA        NA        NA        NA        NA
[50]        NA
 [1]        NA        NA        NA        NA 0.7759501 0.8255090 0.7120667
 [8] 0.6951577 0.7542180 0.7275316 0.7451503 0.7285545 0.7469617 0.8297034
[15] 0.7741526 0.7519351 0.7644968 0.7729314 0.7364307 0.7507919 0.7464788
[22] 0.8685851 0.9373643 0.8237068 0.7730725 0.7252644 0.6746505 0.6903091
[29] 0.7075686 1.0142540 1.2522175 1.2396196 1.1698218 1.2490693 1.1954656
[36] 1.2237815 1.2674711 1.1768190 1.2584819 0.9966886 0.8634196 0.7858126
[43] 0.9126845 0.8671883 0.9990682        NA        NA        NA        NA
[50]        NA
 [1] 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
 [8] 0.0000000 0.0000000 0.7759501 0.8255090 0.7120667 0.6951577 0.7542180
[15] 0.7275316 0.7451503 0.7285545 0.7469617 0.8297034 0.7741526 0.7519351
[22] 0.7644968 0.7729314 0.7364307 0.7507919 0.7464788 0.8685851 0.9373643
[29] 0.8237068 0.7730725 0.7252644 0.6746505 0.6903091 0.7075686 1.0142540
[36] 1.2522175 1.2396196 1.1698218 1.2490693 1.1954656 1.2237815 1.2674711
[43] 1.1768190 1.2584819 0.9966886 0.8634196 0.7858126 0.9126845 0.8671883
[50] 0.9990682

lambda.tools documentation built on May 2, 2019, 4:28 a.m.