| sample_na_loc | R Documentation |
Sample matrix indices for NA injection while respecting row and column
missingness limits and avoiding zero-variance columns.
sample_na_loc(
obj,
n_cols = NULL,
n_rows = 2L,
num_na = NULL,
n_reps = 1L,
rowmax = 0.9,
colmax = 0.9,
na_col_subset = NULL,
max_attempts = 100
)
obj |
A numeric matrix. |
n_cols |
Integer or |
n_rows |
Integer. Target number of missing values to inject per selected column. |
num_na |
Integer or |
n_reps |
Integer. Number of independent repetitions. |
rowmax |
Numeric scalar between |
colmax |
Numeric scalar between |
na_col_subset |
Optional integer or character vector restricting which columns are eligible for missing-value injection. |
max_attempts |
Integer. Maximum number of resampling attempts per repetition before giving up. |
The function uses a greedy stochastic search for valid NA locations. It
ensures that:
Total missingness per row and column does not exceed rowmax and
colmax.
At least two distinct observed values are preserved in every affected column.
A list of length n_reps. Each element is a two-column integer
matrix with row and column indices for sampled NA locations.
set.seed(123)
mat <- matrix(runif(100), nrow = 10)
# Sample 5 missing values across 5 columns
locs <- sample_na_loc(mat, n_cols = 5, n_rows = 1)
locs
# Inject the missing values from the first repetition
mat[locs[[1]]] <- NA
mat
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.