2
votes

This link answers a part of my question: How to randomize (or permute) a dataframe rowwise and columnwise?.

> df1
  a b c
1 1 1 0
2 1 0 0
3 0 1 0
4 0 0 0

Column-wise shuffle gives me below output df3, which is reordering the columns

> df3 <- df1[,sample(ncol(df1))]
> df3
  c a b
1 0 1 1
2 0 1 0
3 0 0 1
4 0 0 0

What I want is that the column names should change as well. Row-wise and column-wise total remains the same, just the column names get reassigned. Something like df4. How can I achieve this?

> df4
  c a b
1 1 1 0
2 1 0 0
3 0 1 0
4 0 0 0

PS: How do I keep the df in its shape rows by column? when I post the question the formatting collapses?

1

1 Answers

3
votes

You might want to just sample the column-names. Something like:

names(df) <- names(df)[sample(ncol(df))]