Learn R Programming

vazul (version 1.0.0)

scramble_variables_rowwise: Scramble values across multiple columns rowwise in a data frame

Description

For each row, shuffle values across the selected columns. All selections passed via ... are combined into a single set and scrambled together. To scramble different variable groups separately, call the function multiple times.

Usage

scramble_variables_rowwise(data, ...)

Value

A data frame with values scrambled rowwise within the selected columns.

Arguments

data

A data frame.

...

Columns to scramble 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("day_"))

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

If data is already a grouped dplyr data frame, existing grouping is ignored.

Details

Rowwise scrambling moves values between columns, so selected columns must be type-compatible. This function requires all selected columns to have the same class (or be an integer/double mix). For factors, the selected columns must also have identical levels.

See Also

scramble_values for scrambling a single vector, and scramble_variables for scrambling multiple variables.

Examples

Run this code
df <- data.frame(
  day_1 = c(1, 4, 7),
  day_2 = c(2, 5, 8),
  day_3 = c(3, 6, 9),
  score_a = c(10, 40, 70),
  score_b = c(20, 50, 80),
  id = 1:3
)

set.seed(123)
# Scramble one set of variables
library(dplyr)
df |> scramble_variables_rowwise(starts_with("day_"))

# Using character vectors
df |> scramble_variables_rowwise(c("day_1", "day_2", "day_3"))

# Scramble multiple sets separately
df |>
  scramble_variables_rowwise(starts_with("day_")) |>
  scramble_variables_rowwise(c("score_a", "score_b"))

# Multiple selectors are combined into one set (values can move between day_* and score_*)
df |> scramble_variables_rowwise(starts_with("day_"), starts_with("score_"))

Run the code above in your browser using DataLab