Skip to contents

Replaces existing elements with your supplied value(s) at random locations.

Usage

insert_value(x, value, prob = 1, n = 1L, keep_nonfinite = TRUE, replace = TRUE)

Arguments

x

(Atomic vector)
The vector of interest.

value

(Atomic vector)
Value(s) to be inserted into x. Must be same type as x or some special value such as NA/NaN/Inf. Will be randomly sampled from for insertion into x.

prob

(Scalar numeric: [0, 1])
The probability that elements from value will be inserted into x.

n

(Scalar integer: [1, Inf])
The number of times value should be sampled from and then inserted into x.

keep_nonfinite

(Scalar logical: c(TRUE, FALSE))
Whether or not to protect non finite values from being replaced in x. TRUE (default) protects NA/NaN/Inf values from being replaced.

replace

(Scalar logical: c(TRUE, FALSE))
Whether or not to sample from value with replacement. TRUE (default) samples with replacement.

Value

vector

Examples

#----------------------------------------------------------------------------
# insert_value() examples
#----------------------------------------------------------------------------
library(bkcheck)

x <- c(1, 2, 3)
insert_value(x = x, value = 10, prob = 1)
#> [1]  1  2 10
insert_value(x = x, value = 11:20, prob = 1)
#> [1]  1  2 16
insert_value(x = x, value = 11:20, n = 3, prob = 1)
#> [1] 14 19 16
insert_value(x = x, value = NA, prob = 1)
#> [1] NA  2  3
insert_value(x = x, value = NaN, prob = 1)
#> [1]   1   2 NaN
insert_value(x = x, value = Inf, prob = 1)
#> [1]   1   2 Inf