Learn R Programming

vazul (version 1.0.0)

mask_names: Mask variable names with anonymous labels

Description

Assigns new masked names to selected variables in a data frame. All selected variables are combined into a single set and renamed with a common prefix. To mask different variable groups with different prefixes, call the function separately for each group.

Usage

mask_names(data, ..., prefix)

Value

A data frame with the specified variables renamed to masked names.

Arguments

data

A data frame.

...

Columns to mask using tidyselect semantics. All arguments are combined into a single set. Each can be:

  • Bare column names (e.g., var1, var2)

  • A tidyselect expression (e.g., starts_with("treatment_"))

  • A character vector of column names (e.g., c("var1", "var2"))

prefix

character string to use as prefix for masked names. This becomes the base prefix, with numeric suffixes appended (e.g., prefix = "treatment_" produces "treatment_01", "treatment_02", etc.). The prefix is used as-is, so include a separator (e.g., underscore) if desired.

See Also

mask_labels for masking values in a vector, mask_variables for masking values in multiple variables, and mask_variables_rowwise for rowwise value masking.

Examples

Run this code
df <- data.frame(
  treat_1 = c(1, 2, 3),
  treat_2 = c(4, 5, 6),
  outcome_a = c(7, 8, 9),
  outcome_b = c(10, 11, 12),
  id = 1:3
)

# Mask one set of variables
library(dplyr)
mask_names(df, starts_with("treat_"), prefix = "A_")

# Using character vectors
mask_names(df, c("treat_1", "treat_2"), prefix = "A_")

# Mask multiple sets separately
# Note that the order of masking matters
# Try to mix up the order of prefixes
# for different sets to ensure proper masking.
df |>
  mask_names(starts_with("treat_"), prefix = "B_") |>
  mask_names(starts_with("outcome_"), prefix = "A_")

# Example with the 'williams' dataset
data(williams)
set.seed(42)

williams |>
  mask_names(starts_with("SexUnres"), prefix = "A_") |>
  mask_names(starts_with("Impul"), prefix = "B_") |>
  colnames()

Run the code above in your browser using DataLab