Learn R Programming

vazul (version 1.0.0)

mask_variables_rowwise: Mask categorical labels across multiple columns rowwise in a data frame

Description

For each row, independently mask labels within the selected columns. All selected columns are combined into a single set and processed together. To mask different variable groups separately, call the function multiple times.

Usage

mask_variables_rowwise(data, ..., prefix = "masked_group_")

Value

A data frame with labels masked rowwise within the selected columns.

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("treat_"))

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

prefix

character string to use as prefix for masked labels. Default is "masked_group_"

See Also

mask_labels for masking a single vector, mask_variables for masking multiple variables, and mask_names for masking variable names.

Examples

Run this code
df <- data.frame(
  treat_1 = c("control", "treatment", "placebo"),
  treat_2 = c("treatment", "placebo", "control"),
  treat_3 = c("placebo", "control", "treatment"),
  condition_a = c("A", "B", "A"),
  condition_b = c("B", "A", "B"),
  id = 1:3
)

set.seed(1037)
# Mask one set of variables
library(dplyr)
df |> mask_variables_rowwise(starts_with("treat_"))

# Using character vectors
df |> mask_variables_rowwise(c("treat_1", "treat_2", "treat_3"))

# Mask multiple sets separately
df |>
  mask_variables_rowwise(starts_with("treat_")) |>
  mask_variables_rowwise(c("condition_a", "condition_b"))

# Example with custom prefix
df |> mask_variables_rowwise(starts_with("treat_"), prefix = "group_")

Run the code above in your browser using DataLab