add_data: Add Random Data to the Ends of a Vector

View source: R/add_data.R

add_dataR Documentation

Add Random Data to the Ends of a Vector

Description

Generates and appends additional random values to the left and/or right ends of a numeric vector x. The range from which these values are drawn is determined by a specified "box" and a scaling factor.

Usage

add_data(x, box, n = c(0, 1), range = c(0, 1))

Arguments

x

numeric Data vector to which new values will be added.

box

character or numeric Defines the base range ("box") used for value generation. See Details.

n

numeric Number of values to add to each side of x. Defaults to c(0, 1).

range

numeric Multiplicative factors of box length for determining value generation intervals. Defaults to c(0, 1).

Details

The "box" defines the central range of the data and can be:

  • "boxplot" — uses the 25th and 75th percentiles (quantile(x, c(0.25, 0.75), na.rm = TRUE)).

  • "range" — uses the full range of the data (range(x, na.rm = TRUE)).

  • A numeric vector of length two — used directly as the box boundaries.

The box length is the distance between the lower and upper box boundaries. The range parameter specifies how far left or right new values can be drawn, as a multiple of the box length.

For the left side, values are drawn uniformly from:

[ \text{box[1]} - \text{range[2]} \times \text{box length},\; \text{box[1]} - \text{range[1]} \times \text{box length} ]

For the right side, values are drawn uniformly from:

[ \text{box[2]} + \text{range[1]} \times \text{box length},\; \text{box[2]} + \text{range[2]} \times \text{box length} ]

The n parameter controls how many values are added:

  • Single number — adds that many values to the right side only.

  • Length-two vector — n[1] values to the left, n[2] values to the right.

Value

A numeric vector containing the original values from x plus the newly generated values.

Examples

x <- rnorm(8)

# Add one value to the right
add_data(x, "box", range = 1.5)

# Add one value to the right using data range
add_data(x, "range", range = 0.1)

# Add one value to the right, larger possible range
add_data(x, "box", range = c(1.5, 3))

# Add two values to the right
add_data(x, "range", n = 2, range = 0.1)

# Add two values to the left and three to the right
add_data(x, "range", n = c(2, 3), range = 0.1)

exams.forge documentation built on Aug. 21, 2025, 5:41 p.m.