Insert a value into a vector
insert_value.RdReplaces existing elements with your supplied value(s) at random locations.
Arguments
- x
(Atomic vector)
The vector of interest.- value
(Atomic vector)
Value(s) to be inserted intox. Must be same type asxor some special value such asNA/NaN/Inf. Will be randomly sampled from for insertion intox.- prob
(Scalar numeric:
[0, 1])
The probability that elements fromvaluewill be inserted intox.- n
(Scalar integer:
[1, Inf])
The number of timesvalueshould be sampled from and then inserted intox.- keep_nonfinite
(Scalar logical:
c(TRUE, FALSE))
Whether or not to protect non finite values from being replaced inx.TRUE(default) protectsNA/NaN/Infvalues from being replaced.- replace
(Scalar logical:
c(TRUE, FALSE))
Whether or not to sample fromvaluewith replacement.TRUE(default) samples with replacement.
See also
Other simulate:
rand_chars(),
sim_character(),
sim_data.frame(),
sim_date(),
sim_datetime(),
sim_double(),
sim_factor(),
sim_integer(),
sim_logical()
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