I don't know if you realize it, but what you are requesting is to actually shuffle all 3 columns together "in the same way " (i.e. am, gear and carb) and then keep all other columns as they are. The function:
- Creates a sequence of integers from 1 to the number of rows of the dataset. This is essentially a vector of indices.
ids <- seq_len(nrow(.data))
- generates
n permutations of the vector of IDs
n_ids <- rerun(n, sample(ids))
-
shuffles the specified columns inside the dataset (without affecting the other columns)
map_dfr(n_ids, function(x){
.data[ids, cols_ids] <- .data[x, cols_ids]
.data
})
You could try it on a sample manually-made dataset to see how it works.