# Create example data
df <- data.frame(
treatment = c("control", "intervention", "control"),
outcome = c("success", "failure", "success"),
score = c(1, 2, 3) # numeric, won't be masked
)
set.seed(123)
# Independent masking for each variable (default - uses column names as
# prefixes)
# Using bare names
mask_variables(df, treatment, outcome)
# Or using character vector
mask_variables(df, c("treatment", "outcome"))
set.seed(456)
# Shared masking across variables
mask_variables(df, c("treatment", "outcome"), .across_variables = TRUE)
# Using tidyselect helpers
mask_variables(df, where(is.character))
# Example with multiple categorical columns
df2 <- data.frame(
group = c("A", "B", "A", "B"),
condition = c("ctrl", "test", "ctrl", "test")
)
set.seed(123)
result <- mask_variables(df2, c("group", "condition"))
print(result)
# Example with williams dataset (multiple categorical columns)
data(williams)
set.seed(456)
# Using bare names (recommended for interactive use)
williams_masked <- mask_variables(williams, subject, ecology)
head(williams_masked[c("subject", "ecology")])
Run the code above in your browser using DataLab