| Random integer values simulation | R Documentation |
Random integer values simulation.
Sample.int(n, size = n, replace = FALSE)
Sample(x, size = length(x), replace = FALSE)
colSample(x, size = rep_len(nrow(x), ncol(x)),
replace = rep_len(FALSE, ncol(x)), parallel = FALSE, cores = 0)
rowSample(x, size = rep_len(ncol(x), nrow(x)),
replace = rep_len(FALSE, nrow(x)), parallel = FALSE, cores = 0)
x |
A numeric vector for sampling or a matrix for column-row sampling. |
n |
This must be an integer value. The function will then draw random integer values from 1:n. |
size |
The number of integer values to sample. |
replace |
Do you want to sample with replacement? If yes, set this equal to TRUE. |
parallel |
Do you want to do it in parallel, for column - row major, in C++? TRUE or FALSE. |
cores |
Number of cores to use for parallelism. Valid only when argument parallel is set to TRUE. Default value is 0 and it means the maximum supported cores. |
These functions provide flexible sampling utilities similar in purpose to R's base functions sample.int and sample. Each function operates on a different structure:
Sample.int: Generates a random sample of integers from 1 to n.
Sample: Samples elements from the vector x.
colSample: Performs column-wise sampling on a matrix or data frame, selecting size[i] elements from each column i.
rowSample: Performs row-wise sampling on a matrix or data frame, selecting size[i] elements from each row i.
All functions support sampling with or without replacement. Parallel versions do not support seeding.
Sample.int, Sample |
A vector of sampled values. |
colSample, rowSample |
A matrix or data frame containing the sampled elements. |
R implementation and documentation: Manos Papadakis <papadakm95@gmail.com>.
sample, sample.int
# Sample integers from 1 to 10 with replacement (faster than base::sample.int)
x <- Sample.int(10, 1000, replace = TRUE)
# Sample from the vector 'x' (faster than base::sample)
xs <- Sample(x)
# Create a matrix and perform column-wise sampling
# 'size' must have the same length as the number of columns
mat <- matrix(1:20, nrow = 5, ncol = 4)
colSample(mat, size = rep(5, ncol(mat)), replace = rep(TRUE, ncol(mat)))
# Create a matrix and perform row-wise sampling
# 'size' must have the same length as the number of rows
rowSample(mat, size = rep(4, nrow(mat)), replace = rep(FALSE, nrow(mat)))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.