2

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?

Community
  • 1
  • 1
Rav
  • 25
  • 5

1 Answers1

3

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

names(df) <- names(df)[sample(ncol(df))]
989
  • 12,579
  • 5
  • 31
  • 53
  • Thanks a lot! It was great and quick help. @P Lapointe, could you please point me in the right direction for df formatting? – Rav Apr 17 '17 at 17:21
  • @Rav Although you tagged another user, what do you mean by your question? – 989 Apr 17 '17 at 17:26
  • 1
    @P Lapointe edited my question to bring the df in correct format. Please see my PS comment. I just wanted know so I don't repeat that again. – Rav Apr 17 '17 at 17:30
  • @Rav Aha. Whenever you copy-paste your data or code from R to here, just select the text and press `Ctrl + k`. Then you will see the correct format here – 989 Apr 17 '17 at 17:32
  • 1
    Great, Thanks a lot @989! – Rav Apr 17 '17 at 17:35