make_random: Add new column with random numbers

View source: R/make_random.R

make_randomR Documentation

Add new column with random numbers

Description

Adds new column with random numbers. The function is built to be able to use it in piping.

Usage

make_random(data, colname = "random", seed = -1, init_seed = FALSE)

Arguments

data

[data.frame]
Data to which a column with random number should be added.

colname

[character(1)]
The name of the new column with the random number. Defaults to "random".

seed

[numeric(1)]
The initializing seed. Defaults to -1, see set.seed.

init_seed

[logical(1)]
Should the seed be initialized. Defaults to FALSE.

Details

To make reproducible random numbers the seed can be initialized with a specific value. The first time the seed is used, set init_seed = TRUE. Thereafter, use init_seed = FALSE if more random numbers are generated in the session to avoid overlapping random numbers.

Value

data.frame with a new column with random numbers.

Author(s)

Petter Hopp Petter.Hopp@vetinst.no

Examples

## Not run: 
# Add column with random variables
x <- as.data.frame(c(1:10))
seed <- 12345

# Initialize with seed first time used
x <- make_random(x, seed = seed, init_seed = TRUE)

# Do not initialize the seed thereafter to avoid overlapping
x <- make_random(x, seed = seed, init_seed = FALSE)

# If you initialize again you get overlapping seeds
x <- make_random(x, seed = seed, init_seed = TRUE)

## End(Not run)


NorwegianVeterinaryInstitute/OKplan documentation built on Dec. 20, 2024, 10:41 a.m.