sean sean - 3 months ago 51
R Question

Shuffle (randomize) data for each row

I am trying to reorder/shuffle the values for each row of a dataframe but cannot figure out how to do this. Please see the example below:

df1 #original data
# X1 X2 X3 X4 X5
#1 1 2 3 4 5
#2 6 7 8 9 10
#3 11 12 13 14 15


The resulting dataframe should look something like this (no pattern).

df2 #each row is randomly shuffled
# X1 X2 X3 X4 X5
#1 2 4 1 5 3
#2 6 10 9 7 8
#3 15 13 11 14 12


I've tried using the sample() example here but its not quite what I want. The "shuffle column-wise" is the most similar to what I'm trying to do but I want to perform sample() for each individual row, not the entire column.

Answer

We can use data.table

library(data.table)
setDT(df1)[, as.list(sample(unlist(.SD))), .(rn = 1:nrow(df1))][, rn := NULL][]

NOTE: The OP mentioned in the post that it is rowwise sampling.

And base R solution:

df1[] <- t(apply(df1, 1, sample))